vb.net 真随机数生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19672483/
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
True Random Number Generating
提问by user2255552
So basically, I have this function that is supposed to generate two random integers between a high and a low number, to make a point on the form. I know that Random can handle this, but Random has consistency, whereas I need the numbers to be completely random on the form.
所以基本上,我有这个函数,它应该在高数和低数之间生成两个随机整数,以在表格上指出一个点。我知道 Random 可以处理这个问题,但 Random 具有一致性,而我需要表单上的数字完全随机。
For example, most of my generated points appear in a diagonal line. This is what I want to avoid. It should go all over the form between the high and low numbers.
例如,我生成的大部分点都出现在对角线上。这是我想要避免的。它应该遍及高低数字之间的整个表格。
Here is my current function:
这是我目前的功能:
Function GetNewLocation() As Point
Randomize()
Dim int1 As Integer = RandomNumber(6, 345)
Randomize()
Dim int2 As Integer = RandomNumber(35, 286)
Return New Point(int1, int2)
End Function
Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
Randomize()
Return New Random().Next(low, high)
End Function
How can I get true random number generation where the point is not on a diagonal line?
如何在点不在对角线上的情况下生成真正的随机数?
回答by Enigmativity
Each time you create a new instance of Randomyou are resetting the random number generator. Since the default constructor uses Environment.TickCountas the seed you are often returning precisely the same sequence of pseudo-random numbers. The system does not update TickCountthat often. This is why it seems to you that you are getting non-random numbers.
每次创建一个新实例时,Random都会重置随机数生成器。由于默认构造函数Environment.TickCount用作种子,因此您经常返回完全相同的伪随机数序列。系统不会TickCount经常更新。这就是为什么在您看来您得到的是非随机数。
Try changing your code like this:
尝试像这样更改您的代码:
Private _rnd As New Random()
Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
Return _rnd.Next(low, high)
End Function
回答by Havenard
There is no true randomness in computer systems. There are many algorithm to generate "random" numbers but they are always based on a seed, like the time, process id, a mix of both, or on more advanced cases where randomness is taken seriously, on sounds being detected in a microphone, data from atmospheric sensors or cosmic microwave background, but it will always derivate from some existing source of information.
计算机系统中没有真正的随机性。有很多算法可以生成“随机”数,但它们总是基于种子,比如时间、进程 ID、两者的混合,或者在更高级的情况下,随机性被认真对待,在麦克风中检测到的声音,来自大气传感器或宇宙微波背景的数据,但它总是来自一些现有的信息源。
回答by user5480760
Thats also not the best slution. I prefer my own code:
那也不是最好的解决方案。我更喜欢我自己的代码:
Dim old As Integer = 6572
Public Function Rand(ByVal min As Integer, ByVal max As Integer) As Integer
Dim random As New Random(old + Date.Now.Millisecond)
old = random.Next(min, max + CInt(IIf(Date.Now.Millisecond Mod 2 = 0, 1, 0)))
Return old
End Function
Thats a little bit better
那好一点
回答by delta2echo
I literally just came up with this solution. Maybe it will help =)
我真的只是想出了这个解决方案。也许它会有所帮助 =)
I went from:
我从:
Dim rand As Random = New Random(DateTime.Now.Millisecond)
To
到
Dim rand As Random = New Random(DateTime.Now.Millisecond * DateTime.Now.Second * DateTime.Now.Minute * DateTime.Now.Hour)
And it seems to have worked really well. You can see the difference in the side by side.
它似乎运作得非常好。您可以看到并排的差异。


回答by Nowrose Muhammad Ragib
in java(assuming the limit is (limit-1)):
在java中(假设限制是(limit-1)):
random = System.currentTimeMillis()%limit;
You get the idea :)
你明白了:)

