调用Rnd()生成相同的数字 [英] Call to Rnd() generating the same number

查看:145
本文介绍了调用Rnd()生成相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我设置这段代码时,每次调试软件时,它都会生成相同的编号.谁能告诉我为什么会这样吗?

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)

  • 我调试软件
  • 代码运行,并生成数字70
  • 我停止调试
  • 我再次调试它,它再次生成数字70

这种情况一遍又一遍地发生,前两次我以为这只是运气,但是当我几次这样做时,它总是回到70(例如).

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).

但是当我保持软件运行并通过使用按钮一次又一次地运行代码时,它会生成完全不同的随机数.重新启动它,然后再次出现数字70.

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.

推荐答案

您需要致电

Randomize()

在调用Rnd()初始化随机数生成器之前.如果不这样做,那么每次运行该程序时,您都会得到相同的数字序列.

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.

示例:

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

原因是Rnd()将始终使用相同的种子来开始序列.如果您想了解更多信息,请在此处进行很好的解释: https://msdn.microsoft.com/zh-CN/library/8zedbtdt(v = vs.90).aspx

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

这篇关于调用Rnd()生成相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆