Jump to content

How do you generate random numbers?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
23 replies to this topic

#1
leroy

leroy

    Newbie

  • Members
  • Pip
  • 4 posts
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?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1 = Random

    End Sub


#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
Int(Rnd()*5)+1;

You do the rest.

#3
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts

chili5 said:


Int(Rnd()*5)+1;


You do the rest.
Don't forget randomize():
randomize()

Int(Rnd()*[I]max number to generate[/I]);


#4
Guest_h4x_*

Guest_h4x_*
  • Guests
rdtsc and al should contain very random data.

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
That is probably it. I'm sure that it has something to do with the time.

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
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
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

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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
Wow I changed my sig!

#10
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
Well that is interesting to know. Where did you learn that? Nothing wrong with being corrected. :)

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
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
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Besides time what else would/could you use because a user given int?