Valhalla Legends Forums Archive | Visual Basic Programming | Curious: Randomization

AuthorMessageTime
FrostWraith
How is this achieved. I have all these fine a dandy function to call.  Could anyone show a way, if possible, this could be done in VB?
September 12, 2006, 1:12 AM
JoeTheOdd
What do you want to randomize? For a number between zero and 100, use this code.

[code]Function Random1to100()
    Randomize
    Random1to1000 = Int(Rnd * 100)
End Function[/code]
September 12, 2006, 2:19 AM
Myndfyr
Except replace Random1to1000 with Random1to100
September 12, 2006, 2:30 AM
HeRo
[quote author=FrostWraith link=topic=15669.msg157778#msg157778 date=1158023538]
How is this achieved. I have all these fine a dandy function to call.  Could anyone show a way, if possible, this could be done in VB?
[/quote]

Int(Rnd * Max #)
September 12, 2006, 4:01 AM
l2k-Shadow
i like this function i wrote:
[code]
Public Function Rand(ByVal lFrom As Long, ByVal lTo As Long) As Long
    Call Randomize
    Rand = lFrom + Int((Rnd * ((lTo + 1) - lFrom)))
End Function
[/code]
September 12, 2006, 4:16 AM
FrostWraith
No no no. I meant how does this function even work. Like, how would you write code that can randomize. I'm talking about the actual function that does the randomizing (Randomize). Would it be hard to write a new one?
September 12, 2006, 9:07 AM
FrOzeN
Have a read over this, http://computer.howstuffworks.com/question697.htm

The function Rnd([number]) in VB6 is similar to the code in that article,
[quote]int rand()
{
  random_seed = random_seed * 1103515245 +12345;
  return (unsigned int)(random_seed / 65536) % 32768;
}[/quote]
And the function Randomize([number]) generates a new seed. Not sure about where it gets the seed from, but it's probably a combination of the time, mouse cursor, etc. Also, you can use the optional property [number] to define what you want the seed to be. This property also exists in the Rnd() function, I'm not sure of what it does, but it's probably to do with modifying the seed aswell.
September 12, 2006, 9:36 AM
Myndfyr
[quote author=FrostWraith link=topic=15669.msg157786#msg157786 date=1158052028]
No no no. I meant how does this function even work. Like, how would you write code that can randomize. I'm talking about the actual function that does the randomizing (Randomize). Would it be hard to write a new one?
[/quote]
Numbers generated by computers are typically not truly random but really use a pseudorandom number generator, which is a deterministic function.  If you were to give the generator the same seed repeatedly, you would see the same sequence of numbers generated by the random number generator.
September 12, 2006, 7:08 PM
K
[quote author=MyndFyre[vL] link=topic=15669.msg157792#msg157792 date=1158088107]
If you were to give the generator the same seed repeatedly, you would see the same sequence of numbers generated by the random number generator.
[/quote]

Which is actually not true with the Randomize() function.  Successive calls to Randomize with the same value will not result in generating the same numbers with Rnd() unless you first call Rnd() with a negative number to indicate that this is what you wish to acheive.
September 12, 2006, 7:47 PM

Search