Call to Rnd() generating the same number

前端 未结 3 1572
感动是毒
感动是毒 2020-12-06 18:51

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
         


        
3条回答
  •  攒了一身酷
    2020-12-06 19:08

    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

提交回复
热议问题