真随机数生成 [英] True Random Number Generating
问题描述
因此,基本上,我具有此函数,该函数应该在高和低数字之间生成两个随机整数,以在表格上指出一个点.我知道Random可以解决这个问题,但是Random具有一致性,而我需要表格上的数字是完全随机的.
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.
例如,我生成的大多数点都显示在对角线上.这就是我要避免的.它应该遍历高数字和低数字之间的形式.
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.
这是我当前的功能:
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?
推荐答案
每次创建Random
的新实例时,都要重置随机数生成器.由于默认构造函数使用Environment.TickCount
作为种子,因此您通常会返回完全相同的伪随机数序列.系统不会经常更新TickCount
.这就是为什么在您看来您正在获得非随机数的原因.
Each time you create a new instance of Random
you are resetting the random number generator. Since the default constructor uses Environment.TickCount
as the seed you are often returning precisely the same sequence of pseudo-random numbers. The system does not update TickCount
that often. This is why it seems to you that you are getting non-random numbers.
尝试像这样更改代码:
Private _rnd As New Random()
Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
Return _rnd.Next(low, high)
End Function
这篇关于真随机数生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!