简体   繁体   中英

generating random numbers in visual basics

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM