生成随机数的问题 [英] Problems Generating Random Numbers

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

问题描述

我有一个简短的问题,我希望有人可以帮助或至少解释。


发生的事情是我试图在应用程序中使用随机数,按照下面的示例测试代码。当我运行此代码时,显示的随机数对于abot 25个循环都是相同的,然后正常随机化。


可以有人告诉我为什么这是或者至少告诉我我做错了什么。


问候格伦


模块模块1


Sub Main()

Dim rLoop As Integer

对于rLoop = 1到100

Console.WriteLine(返回值{0} - {1} ;,rLoop,RollPercent)

下一页

Console.ReadLine()

结束子


公共函数RollPercent()As Integer

Dim tmpRandom As New Random(Environment.TickCount)

返回tmpRandom.Next(1,101)

结束功能


结束模块

I have a quick Question and I Hope some one can help or at least explain.

What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I run this code the random numbers that are displayed are all the same for abot 25 cycles and then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module

推荐答案

Environment.TickCount返回毫秒,这些毫秒不够精细对于你的方式。所以基本上你用相同的种子创建多个随机数,这当然意味着它们根本不是随机的。


你应该将你的tmpRandom改为私有成员类并初始化它只一次。然后像你现在一样调用tmpRandom.Next。
Environment.TickCount returns milliseconds, which aren''t granular enough for the way you''re going about this. So essentially you are creating multiple random numbers with the same seed, which of course means that they''re not random at all.

You should change your tmpRandom to a private member of the class and initialize it only once. Then call tmpRandom.Next as you are doing now.


我相信它与变量范围有关,如果你创建变量

在函数内部并启动环境值。点击SEED将保持相同直到下一个滴答,相同的种子导致相同的随机。数字。

尝试使用模块级别随机的相同示例:


模块模块1


Dim tmprandom作为New System.Random(Now.Millisecond)

Sub Main()

Dim rLoop As Integer

对于rLoop = 1到100

Console.WriteLine("返回值{0} - {1}",rLoop,RollPercent)

下一页

Console.ReadLine

结束子


公共函数RollPercent()As Integer

''Dim tmpRandom As New Random(Now.Millisecond)

返回tmpRandom.Next(1,101)

结束功能

结束模块

" Glenn Wilson" <胃肠********* @ discussions.microsoft.com>在消息中写道

新闻:CA ********************************** @ microsof t.com ...
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
''Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
我有一个简短的问题,我希望有人可以帮助或至少解释。

发生的事情是我试图随机使用
应用程序中的数字,根据下面的示例测试代码。当我运行此代码时,显示的
随机数对于abot 25个循环都是相同的,然后
然后正常随机化。

有人可以告诉我为什么这样或者至少告诉我我在做什么
错了。

问候格伦

模块模块1

Sub Main()
Dim rLoop as Integer
对于rLoop = 1到100
Console.WriteLine(返回值{0} - {1},rLoop,RollPercent)
下一页
控制台.ReadLine()
End Sub

公共函数RollPercent()As Integer
Dim tmpRandom As New Random(Environment.TickCount)
返回tmpRandom.Next(1, 101)
结束功能

结束模块
I have a quick Question and I Hope some one can help or at least explain.

What is happening is that I am trying to use random numbers in an
application, as per the sample test code below. When I run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing
wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module



谢谢你会去试试

" Jared"写道:
Thanks will give it a go

"Jared" wrote:
我相信它与变量范围有关,如果你在函数内部创建变量
并启动环境值,那么SEED将会尝试使用模块级随机的相同示例,如下所示:

模块模块1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
对于rLoop = 1到100
Console.WriteLine(返回值{0} - {1},rLoop,RollPercent)
下一页
Console.ReadLine
End Sub

公共函数RollPercent()As Integer
''Dim tmpRandom As New Random(Now.Millisecond)
返回tmpRandom.Next(1,101)
结束功能
结束模块

" Glenn Wilson" <胃肠********* @ discussions.microsoft.com>在消息中写道
新闻:CA ********************************** @ microsof t.com。 ..
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
''Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
我有一个简短的问题,我希望有人可以帮助或至少解释。

我正在尝试使用随机数
应用程序,根据下面的示例测试代码。当我运行此代码时,显示的
随机数对于abot 25个循环都是相同的,然后
然后正常随机化。

有人可以告诉我为什么这样或者至少告诉我我在做什么
错了。

问候格伦

模块模块1

Sub Main()
Dim rLoop as Integer
对于rLoop = 1到100
Console.WriteLine(返回值{0} - {1},rLoop,RollPercent)
下一页
控制台.ReadLine()
End Sub

公共函数RollPercent()As Integer
Dim tmpRandom As New Random(Environment.TickCount)
返回tmpRandom.Next(1, 101)
结束功能

结束模块
I have a quick Question and I Hope some one can help or at least explain.

What is happening is that I am trying to use random numbers in an
application, as per the sample test code below. When I run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing
wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module




这篇关于生成随机数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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