vb.net 调用 Rnd() 生成相同的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34644576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to Rnd() generating the same number
提问by Wietse292 aka SnAKE
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
- 我调试软件
- 代码运行,并生成数字 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.
但是当我保持软件运行并一遍又一遍地运行代码时,通过使用一个按钮,它会生成完全不同的随机数。再次启动它,再次出现数字70。
采纳答案by Jorge Torres
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.
在调用 Rnd() 初始化随机数生成器之前。如果不这样做,则每次运行该程序时,您都会得到相同的数字序列。
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
原因是 Rnd() 将始终使用相同的种子来启动序列。如果你想阅读更多关于它的信息,这里有很好的解释:https: //msdn.microsoft.com/en-us/library/8zedbtdt(v=vs.90).aspx
回答by David Wilson
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.
进入您的代码的 _load 事件。这会根据时间更改种子。
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.
或者,您可以使用以下代码,因为这不需要每次运行程序时都调用“随机化”,并且更容易控制生成的数字范围。例如,不是按照下面的代码在 0 到 100 范围内的随机数,您可以选择生成从 45 到 967 或任何其他您喜欢的范围的数字,只需更改第二行的参数即可。
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.
将 randomGenerator 声明为项目范围的变量而不是在代码块中重新声明它可能更好 - 这是因为它使用时间作为种子。
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
回答by dbasnett
Since this was tagged as .Net you should not be using VB6 legacy functions. For .Net use the Random ClassHere is an example that requires a button and a label.
由于这被标记为 .Net,因此您不应使用 VB6 遗留函数。对于 .Net 使用Random Class下面是一个需要按钮和标签的示例。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = GenRandInt(100).ToString
End Sub
Private Shared prng As New Random 'only need one - should not be in a method
Private Function GenRandInt(maxValue As Integer) As Integer
'returns an integer between 0 and maxValue inclusive
Return prng.Next(maxValue + 1)
End Function
End Class
Read the documentation for the details and other uses of the Random class.
阅读文档以了解 Random 类的详细信息和其他用途。
edit: If for some reason you decide to continue to use Rnd, then call Randomize before using it.
编辑:如果由于某种原因您决定继续使用 Rnd,请在使用前调用 Randomize。

