Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1 = Random End Sub
How do you generate random numbers?
Started by leroy, Aug 30 2009 03:18 PM
23 replies to this topic
#1
Posted 30 August 2009 - 03:18 PM
Im a noob. Ive got one one button and one textbox. Whats the code so when you press the button a random number shows in the textbox?
|
|
|
#2
Posted 30 August 2009 - 04:42 PM
Int(Rnd()*5)+1;
You do the rest.
#3
Posted 30 August 2009 - 05:15 PM
chili5 said:
Int(Rnd()*5)+1;
You do the rest.
randomize() Int(Rnd()*[I]max number to generate[/I]);
#4
Guest_h4x_*
Posted 30 August 2009 - 06:00 PM
Guest_h4x_*
rdtsc and al should contain very random data.
#5
Posted 30 August 2009 - 06:04 PM
How does a computer really generate something randomly though? Computers have to be totally logic they cannot think, right? So how is something ever really random? There has got to be some weird logic behind it, right? The only thing I can think of is to do some weird thing with the same of the day, times it by some number, hash it get the first x amount of letters, add them together, then %10 it or something...
#6
Posted 31 August 2009 - 02:36 AM
That is probably it. I'm sure that it has something to do with the time.
Then the nextDouble method returns a double between 0.0 and 1.0.
:) I don't know how the bit shifts work but if you did, you could probably figure it out.
This is how Java creates the seed for the function. Yes, by using a time function.
BTW, How this function works:
Rnd() returns a floating value between 0 and 1. Then I multiply it by some number. This number is the highest possible value.
Consider that Rnd returns 1.0 multiplying it by 10 produces 10.0 and then I truncate the decimal resulting it 10. Now adding one gives me 11 which is the highest value possible.
Similary, if Rnd returns 0.0 multiplying it by 10 produces 0.0 and then truncating the decimal results in 0. Adding one gives me 1 as the smallest value possible.
public Random() { this(++seedUniquifier + System.nanoTime()); }
private static volatile long seedUniquifier = 8682522807148012L;
Then the nextDouble method returns a double between 0.0 and 1.0.
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27))
/ (double)(1L << 53);
}
:) I don't know how the bit shifts work but if you did, you could probably figure it out.
This is how Java creates the seed for the function. Yes, by using a time function.
BTW, How this function works:
Rnd() returns a floating value between 0 and 1. Then I multiply it by some number. This number is the highest possible value.
Consider that Rnd returns 1.0 multiplying it by 10 produces 10.0 and then I truncate the decimal resulting it 10. Now adding one gives me 11 which is the highest value possible.
Similary, if Rnd returns 0.0 multiplying it by 10 produces 0.0 and then truncating the decimal results in 0. Adding one gives me 1 as the smallest value possible.
#7
Posted 31 August 2009 - 07:27 AM
Wait, if it did it based on time, and we called 5 random numbers in the same second... those should really return the same number?
#8
Posted 31 August 2009 - 07:30 AM
No because the random seed uses nano seconds not seconds. So if you use 5 random numbers in the same nanosecond, they would be the same. :P
Good luck with that. :P
Good luck with that. :P
#9
Posted 31 August 2009 - 12:04 PM
Neither are right. Even if you did somehow call 5 pseudorandom numbers at the same nanosecond, you still wouldn't have 5 similar numbers. The way pRNG's work is by providing them with a single seed value, which can be anything, then on each subsequent call to the algorithm it simply returns the next value in a very large sequence.
A popular random number generator is the Mersenne Twister. The most popular version is MT19937, which has a "period" of 219937. A period is how long of a sequence of different pseudorandom numbers are generated by the pRNG, so if you were to (somehow) call 219937 numbers, the program would begin returning identical values as to what was returned at the beginning of the program, and would continue to do so. A "seed" value is used to (really basically) select a point somewhere on that pseudorandom sequence to start counting from, and nothing more. So if you gave the program the same seed (let's just say.. oh... 500), you'd end up with precisely the same set of numbers each time you ran the program. Go ahead and try it in any language you know, using any (non-cryptographically secure) pRNG, in any language. Print out 100 values starting from seed 500, they'll always be the same. :P
A popular random number generator is the Mersenne Twister. The most popular version is MT19937, which has a "period" of 219937. A period is how long of a sequence of different pseudorandom numbers are generated by the pRNG, so if you were to (somehow) call 219937 numbers, the program would begin returning identical values as to what was returned at the beginning of the program, and would continue to do so. A "seed" value is used to (really basically) select a point somewhere on that pseudorandom sequence to start counting from, and nothing more. So if you gave the program the same seed (let's just say.. oh... 500), you'd end up with precisely the same set of numbers each time you ran the program. Go ahead and try it in any language you know, using any (non-cryptographically secure) pRNG, in any language. Print out 100 values starting from seed 500, they'll always be the same. :P
Wow I changed my sig!
#10
Posted 31 August 2009 - 01:24 PM
Well that is interesting to know. Where did you learn that? Nothing wrong with being corrected. :)
#11
Posted 31 August 2009 - 07:34 PM
There is no real random number generator in computing, just pseudorandom. Basically some mathematician comes up with some crazy complicated math function that takes a single initial value and just steps through each input value every time you call it, giving a seemingly random series of output numbers. (Lorenz equations, due to their high sensitivity to initial conditions, work well for this.) If you were to seed a pseudorandom number generator with the exact same value twice, you'd get the exact same sequence of numbers both times.
sudo rm -rf /
#12
Posted 31 August 2009 - 07:38 PM
Besides time what else would/could you use because a user given int?


Sign In
Create Account

Back to top










