Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to Rnd() generating the same number

Tags:

random

vb.net

When I set up this bit of code, every time I debug the software it generates the same number. Can anyone tell me why this is happening?

dim value as integer
value = (CInt(Int(100 * Rnd())))
messagebox.show(value)

Because it should be random. here is an example: (From top to bottom)

  • I debug the software
  • the code runs, and it generates the number 70
  • I stop the debugging
  • I debug it again and it generates the number 70 again

And that's happening over and over, the first two times I thought it was just luck, but when I did it a couple of times it always came back on the 70 (as an example).

But when I keep the software running and I run the code over and over again, by the use of a button, it generates completely different, and random numbers. Start it back up again and there is the number 70 again.

like image 335
wietse Avatar asked Jan 06 '16 23:01

wietse


People also ask

Why is Rand generating the same number?

This is because MATLAB's random number generator is initialized to the same state each time MATLAB starts up. If you wish to generate different random values in each MATLAB session, you can use the system clock to initialize the random number generator once at the beginning of each MATLAB session.

Why does Rand generate the same number in C++?

The random number generator is being seeded with the same default state on each run of your program. In order to get different results on each run, you need to seed the random number generator in your program by calling srand() and passing in a new seed.

What does Rnd () do Visual Basic?

If number is greater than zero, Rnd generates the next random number in the sequence. If number is equal to zero, Rnd generates the most recently generated number. If number is not supplied, Rnd generates the next random number in the sequence.

How do you use RND function?

Description. Use the RND function to generate any positive or negative random integer or 0. expression evaluates to the total number of integers, including 0, from which the random number can be selected. That is, if n is the value of expression, the random number is generated from the numbers 0 through (n - 1).


2 Answers

You need to call

Randomize()

Before you call Rnd() to initialize your random num ber generator. If you don't, every time that you run the program you will get the same sequence of numbers.

Example:

dim value as integer
Randomize()
value = (CInt(Int(100 * Rnd())))
messagebox.show(value)

The reason is that the Rnd() will always use the same seed to start the sequence. If you want to read more about it, is very well explained explained here: https://msdn.microsoft.com/en-us/library/8zedbtdt(v=vs.90).aspx

like image 152
Jorge Torres Avatar answered Sep 30 '22 03:09

Jorge Torres


The reason you get the same random number each time is that when the program runs, it always starts with the same seed number for generating the first random number. To change the seed, you can add this ..

Randomize()

into your code's _load event. This changes the seed based on the time.

Alternatively, you could use the following code as this doesn't need to call 'Randomize' each time the program is run, and it is much easier to control the range of numbers generated. For example instead of random numbers in the range of 0 to 100 as per the code below, you could choose to generate numbers from 45 to 967 or any other range you like, just by changing the parameters of the second line.

Dim randomGenerator As New Random 'add this to the beginning for your Form1 class
value =randomgenerator.Next(0,100) 'add this into your methods as needed
Messagebox.Show(value)

Its probably better to declare randomGenerator as project wide variable rather than keep re-declaring it in a code block - This is because it uses the time as the seed.

If the declaration is in a tight loop that iterates over small intervals of time, the seed might sometimes be the same for each time the variable is declared and you could end up with the same number being generated multiple times. For example - This is how not to do it :-

For i As Integer = 1 To 1000
    Dim a As New Random
    Console.WriteLine(a.Next())
Next
like image 21
David Wilson Avatar answered Sep 30 '22 01:09

David Wilson