Question:
How to use Rnd() function in Visual Basic to generate just one number?
Rinfret
2012-10-04 23:38:25 UTC
Hi there,

I am currently using the Rnd() function in Visual Basic which keeps generating a random value between 0 and 1 inclusive until I stop debugging my code. My question is the following:

How do I use the Rnd() function to generate just ONE value, which I can store? In other words, rather than generating an ongoing list of values between 0 and 1, is there a way of doing it so that it just gives out one value?

Any help would be very much appreciated.
Cheers.
Three answers:
AnalProgrammer
2012-10-04 23:55:37 UTC
One call to the RND() function will return one number in the range 0 to 1.

See the link for more information.



Have fun.
?
2012-10-05 07:40:30 UTC
Visual basic has a Randomize function. What this function does is use the system time to seed the random number generator (Rnd) so that it generates a unique random number. This seed is needed because a random number generator isn't truly random, it uses a number generation algorithm that uses this seed value, which is always different, as part of that algorithm.



I haven't tried this, but according to the documentation on randomize at http://msdn.microsoft.com/en-us/library/8zedbtdt(v=vs.80).aspx , you can specify a seed value as an argument. In theory, if I understand the documentation and Rnd function correctly, if you were to use the same seed value every time, you should repeatedly get the same random number.
James Bond
2012-10-05 07:07:00 UTC
To generate random number between 1 to 100.



Dim value As rval = CInt(Int((100 * Rnd()) + 1))



the variable rval contains a random value between 1 to 100


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...