Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: FrostWraith on September 11, 2006, 08:12 PM

Title: Curious: Randomization
Post by: FrostWraith on September 11, 2006, 08:12 PM
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?
Title: Re: Curious: Randomization
Post by: Joe[x86] on September 11, 2006, 09:19 PM
What do you want to randomize? For a number between zero and 100, use this code.

Function Random1to100()
    Randomize
    Random1to1000 = Int(Rnd * 100)
End Function
Title: Re: Curious: Randomization
Post by: MyndFyre on September 11, 2006, 09:30 PM
Except replace Random1to1000 with Random1to100
Title: Re: Curious: Randomization
Post by: Hero on September 11, 2006, 11:01 PM
Quote from: FrostWraith on September 11, 2006, 08:12 PM
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?

Int(Rnd * Max #)
Title: Re: Curious: Randomization
Post by: l2k-Shadow on September 11, 2006, 11:16 PM
i like this function i wrote:

Public Function Rand(ByVal lFrom As Long, ByVal lTo As Long) As Long
    Call Randomize
    Rand = lFrom + Int((Rnd * ((lTo + 1) - lFrom)))
End Function
Title: Re: Curious: Randomization
Post by: FrostWraith on September 12, 2006, 04:07 AM
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?
Title: Re: Curious: Randomization
Post by: FrOzeN on September 12, 2006, 04:36 AM
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,
Quoteint rand()
{
  random_seed = random_seed * 1103515245 +12345;
  return (unsigned int)(random_seed / 65536) % 32768;
}
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.
Title: Re: Curious: Randomization
Post by: MyndFyre on September 12, 2006, 02:08 PM
Quote from: FrostWraith on September 12, 2006, 04:07 AM
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?
Numbers generated by computers are typically not truly random but really use a pseudorandom number generator (http://en.wikipedia.org/wiki/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.
Title: Re: Curious: Randomization
Post by: K on September 12, 2006, 02:47 PM
Quote from: MyndFyre[vL] on September 12, 2006, 02:08 PM
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.

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.