0

I am using a random number generator in my program, however it keeps returning the same value (0.71) every time i run the program.

code:

number = FormatNumber(Rnd(1), 2)
    rdmlabelTxt.Text = number.ToString

is there a way to produce a different random number when starting the program? thanks.

hasitha w
  • 3
  • 1
  • 2

3 Answers3

1

According to Microsoft "the same number sequence is generated" when you don't give a parameter. The article also suggests to "Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer."

I think this will solve your issue - let us know.

mark
  • 344
  • 3
  • 8
0

You are required to write a for loop to be able to generate different numbers

For i = 1 to 100
number = FormatNumber(Rnd(1), 2)
Cells(i, "A").Value = number
next i
Zapata
  • 133
  • 1
  • 5
  • 20
0

You just have to use Randomize() call before your codes.

Randomize()
Dim number As Double = 0
number = FormatNumber(Rnd(1), 2)
rdmlabelTxt.Text = number.ToString
Gaurang
  • 67
  • 8
  • You should make it clearer that `Randomize()` should only be called once in the whole program. But then your answer will be the same as Mark's, so unfortunately you are not adding any value to this post. – Andrew Morton Jun 12 '16 at 12:03