-
Random Numbers
I want to create an application that you enter the max number in, then on pressing the button will display a number at random between 1 and the max number.
When pressing the button again, it'll do the same but will never pick a number already drawn, or not untill the programe has been re-set.
Has anyone: -
1) Understood what I've typed above?
if so
2) Anyone got any ideas on how to go about it?
Any help/advice appreciated.
Regards
-
Try reading the VB help file.
-
Well, rnd() would give you a random number between 0 and 1, wich multiplied by the max number would give a number between 0 and the max number.
It will however not garantie that it won't generate numbers already generate once before.
So you would properly have to create an array holding all the walyes already generated, and then test the newly generated number against this array. If the number is found generate a new one and test again.
If however max number is very large, it will properly need to generate quite alot of numbers before it generates the last missing number, but that's details.
-
1 Attachment(s)
Here is a little program that i made. Modify it as you like. I only did some inline error handling. So you would have to check for like keypress make sure that it's a number being set for the maxnum. Let me know what you think. By the way here is the formula for getting a random number.
Int((MyNumber* Rnd) + 1)
My Number being the Max Number. You will see how i use it.
-
by the way AIS_DK that is exactly what i am doing in that little app that i included. Of course performance will degrade as the number gets higher, but hey if there is another way ( as i am not the smartest programmer yet!) please let me know. And i will test and adopt the new way. I guess great minds think alike.
-
We understood what you typed, but you don't understand random numbers.
'random' means that you have an equal chance every time of picking any number. That means, just because 13 came up last time, does not mean it cannot come up again.
What you and jjortiz want is a deck shuffle algorithm, maybe with a 'deck' of, say 1000 cards. In this case each card is the 'random' (by your definition) number you want to return. That way, the number you get is never repeated.
Put an array of sequential numbers in random order:
Code:
Option Explicit
Option base 1
Dim where as Integer ' public pointer or number picker
Dim limit as Integer ' this is the upper limit you got from the user
Dim arr() as Integer ' deck of unique numbers in random order
Sub bldDeck()
Dim tmp as integer
Dim i as integer
Dim j as integer
Redim arr(limit)
where = 0 ' set the number picker to the beginning
for i = 1 to limit
arr(i) = i
next ' we now have a bunch of non-repeating,sequential numbers
' now put them in random order
j = limit
Do While j > 1
i = (rnd*j) +1 ' pick a number at random
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp ' the two numbers are swapped
j = j -1 ' make the list one element shorter
Loop
' you now have limit numbers in random order
End Sub
Function getNum() as Integer
where = where + 1
getNum = arr(where) ' this is a randomly ordered number
' from 1 to limit
End Function
-
I think everyone failed to use the Randomize statement.
Randomize
intRand = Int((intNumber * Rnd) + 1)
Without Randomize the random number generator isn't random. Meaning if you start the program again it will give you the same series as numbers again.
Like
3,9,10,3 - 1st Try
3,9,10,3 - 2nd Try
3,9,10,3 - 3rd Try
-
jim I understand what random numbers are. I also understand the fact that a repeated number can come. I just happened to understand what he wanted. So i created an application that did just that. Weather it was the best way or not. That is a matter of opinion and testing. What i did fail to explain was what you explain.
JaredM If you look at my application i did use the randomize statement.
-
I didn't look at your application, because I'm at work.
.EXE = no no
VB Project = Don't have VB at work.
-
Oh! well here is the code.
Code:
Private Function GenRandNum(ByVal MaxNumber As Long) As Long
Dim lngNumber As Long
lngNumber = MaxNumber
GenRandNum = Int((lngNumber * Rnd) + 1)
End Function
-
Where is the: Randomize ?
-
Sorry for some reason i left it out.
Code:
Private Function GenRandNum(ByVal MaxNumber As Long) As Long
Dim lngNumber As Long
lngNumber = MaxNumber
Randomize
GenRandNum = Int(((lngNumber * Rnd) + 1))
End Function
-
Yeah, that's what I thought.
-
It was also left out from the zip above. So i am resubmitting. I copied the code from MSDN. It's been there for the longest. I usually forget about it. It's not every day that you have to produce random number.
Sorry for some reason the Randomize Statement was left out.
JaredM. In the zip is the code and the exe so you can take a look at it. Hey and i made a mistake. I overlooked it. I have seen that randomize statement inside of the MSDN help for the longest. I just happened to forget it. Hey simple mistake.
-
1 Attachment(s)
... and i forgot to attach it.
-
Chill, chill, chill. I'm just kidding, because I'm just kidding, and everyone kids around with someone at some time or another. Then they need to excessively explain it, and continue, but it's ok cause I understand.
Sarcasism aside. I was just pointing it out for the person asking about it. I forgot the Randomize procedure a few times, that's why I stressed it's importance.
-
I was not being sarcastic or anything. I know that you were just kidding.Please do not take it personal. Maybe the way that i express myself comes off wrong. I apologize the misunderstands. It's a common thing.
-
-
What's the question? Did you take a look at the app. Thanks for reminding me. Cause if not it would have giving the same numbers over and over every time the app restarted. Thanks.
-
No problem. My question markk was because of the way you're writing. The flow is kind of throwing me off.
Anyway, it was a little mistake. I just thought I would add to it so the RANDOM number newbie you were helping out didn't encounter the problem.
-
I know i have a monotone way of speaking. Sorry for that i am trying to change that.
-
Is English your native language?
-
Yes it is. I know, i have no grammer and write very poorly.
-
-
jj - no problem. I was cranky with the original poster's tone.
You did what he wanted. I did it another way that put all the computational load in one step, without having to use searches.
Same result.
I just did a thing that has been around as a standard part of Monte Carlo analysis (playing card games)
since 1968.
Jared -
Lighten up.
Randomize DOES NOT do what Pozzi wants. He wants a random sequence of unique numbers. Those are not random numbers.
They are different cases.
And I'm not picking on anybody, but you are picking on jj without cause - because Randomize doesn't do diddly with the problem Pozzi has. He wants to get several thousand numbers, all different, never repeating. Psuedo-Random Number Generators (PRNG) don't do that.
Randomize reseeds the PRNG. You can call Randomize until the cows come home - it will not affect the following:
I just got a 13 from my rnd call. What are my chances of getting 13 again? Answer: the same as they were before the first time you got the 13. The chances of getting 13 (or any number) are supposed to be virtually the same with Rnd*100 + 1 (or whatever) each and every time you call it. It is supposed to be blind to what numbers have come before. Without going off the deep end math-wise, PRNG's are not blind completely. And they are not truly random. Just close enough.
Now, we understand that Randomize isn't gonna help. Most especially if Pozzi only wants a run of like 10 numbers. Hopefully we also got the 'why' part about Randomize, now.
What Pozzi wants is once he gets 13: NEVER use it again. Randomize does not affect that. And doing what Pozzi wants is not generating random numbers. Period.
-
Jim that's no problem. The only thing that randomize is doing in my case is not displaying the same pattern when the app is restarted. The seed (Increment value) can be changed so the randomize statement does not have to be used. The seed automatically picks up from the system time if one is not provided. So if i was to let it go to the system time. Chances of ever getting the same pattern is slim. I figured that the poster was not asking for a random number just to pick numbers from a certain max number not a range. So the range will always be from 0 - MaxNum. I checked out your way of doing it and it is much faster because there is no cross refrencing like i am doing. Your process time is up from where you create the sequential numbers. Well no hard feelings on this side and thanks for sticking up to me. I guess some people do not understand me or just think i am snob. I think that between me and JaredM are settled. It's ok he has not been around the block long enough. Not that i have.
Cheers! <- I see everyone using it. I guess i will get around to asking why one day. Must be a european thing. hehehe.
-
I was kidding of course, but with that aside.
jim mcnamara, you are wrong. Randomize is still needed even in a unique number scenario, because you just opened your card deck to cheating. The first card when the app starts will show you. If the first card is 13 and the person then gets 5 and then gets 9. They'll get the same cards again when you rerun the app. If it's a little more spaced out like in solitaire or something then the results will be based on what I choose to do, but I can still play the same game again just be restarting the app. It will shuffle it the exact same way if you don't use Randomize.