PDA

Click to See Complete Forum and Search --> : Random number generator


Electron
Nov 24th, 1999, 10:08 PM
I am making a program that utilizes a random number generator. The one I am using is from the VB Help files:

T = Int((5 * Rnd) + 1)

I seem to recall that this particular format is not all that random and there is a better way to do it. Does anyone know what that is or have any suggestions?

Thanks!

Jeff

Nov 24th, 1999, 10:13 PM
Funny, I noticed that as well!!

I't seems to need you to have "Randomize" (without the quotes) before you make a call to the RND function. E.G.

Randomize
T = Int((5 * Rnd) + 1)

Must admit, though, the random number is still pretty predictable the fist time.

struntz
Nov 25th, 1999, 04:49 AM
Yes that is true you must put Randomize in the formload but i don't get why do u have to put +1 at the end i have made a random numbers program just by making a function that has

Public Function RandomNumbers()
RandomNumbers = int(Rnd * 10)
end Function

and then i just put this code in the cmdSpin
lbl1.caption = RandomNumbers
lbl2.caption = RandomNumbers
lbl3.caption = RandomNumbers
and it produces Different Random Numbers every time without using the + 1 . . . what does the + 1 do ?


------------------
Cory Sanchez
Young Student
ICQ#: 18640149

Andy Collyer
Nov 25th, 1999, 04:13 PM
I've worked all this out before but unfortunately my VB PC has completely died so I can't get to any of the code :( Think this is right...

Consider this function to generate a random number between known values:

Public Function BoundedRND(ByVal iMin as Integer, ByVal iMax as Integer) as Integer

BoundedRND = Int((iMax - iMin + 1) * Rnd + iMin)

End Function

This gets a random number in a range (iMax - iMin + 1), in your example 0 to 4, then adds the lower number (in your example 1) to put the returned range within the desired range.

In the situation you're talking about, you are after an integer between 1 and 5: applying in BoundedRND gives:

BoundedRND = Int((5 - 1 + 1) * Rnd + 1)

= Int(5 * Rnd + 1)

= 1..5

So - the "+ 1" is just putting the random number so far generated within the bounds 1..n.

Make sense?

AndyC
London

------------------
* * * * * * * * * * * * * * * * * * * * * *
* *
* AndyC *
* London *
* email: andy.collyer@bigfoot.com *
* *
* * * * * * * * * * * * * * * * * * * * * *


[This message has been edited by Andy Collyer (edited 11-26-1999).]