随机无代问题 [英] Random No generation problems
问题描述
如何调用一些
人建议的随机数生成器。理想情况下,我想要做的是实例化随机数。
生成器,其种子值不重复值,并且可以从任何类调用
,因为我必须从
几个不同的类别调用随机数发生器。
这是我的代码:
使用系统;
class cMain
{
static void Main()
{
}
}
公共类cGetRandom
{
静态随机rand = new Random();
private static int GetRandom ()
{
锁定(兰特)
{
返回rand.Next();
}
}
}
公共类cClass1
{
private static int Class1()
{
int lnRand = 0;
lnRand = cGe tRandom.rand.Next(1,49);
}
}
TIA
Roy
将Environment.TickCount传递给Random构造函数。那应该是
为发电机播种并为你提供所需的随机性。
你好Chris,
>
是的我看到类似于Random(Environment.TickCount)的东西是
这样:
随机(未选中)((int) DateTime.Now.Ticks));
但我想知道的是我是否每次都需要创建一个Random
构造函数的实例在类中生成随机数。你需要看看我必须在几个不同的类中生成随机数,所以理想情况下我想要只使用一次构造函数,就像你在C ++中那样使用>
br />
(srand((无符号)时间(NULL))每次都得到一个随机数然后
总是使用同一个实例生成一个随机数。这可能吗?
谢谢
Roy
" Chris Dunaway"< du ****** @ gmail .com>在留言中写道
news:11 ********************** @ g14g2000cwa.googlegr oups.com ...... < blockquote class =post_quotes>将Environment.TickCount传递给Random构造函数。这应该为生成器播种并为您提供所需的随机性。
< blockquote>
你好Chris,
是的我见过类似于Random(Environment.TickCount)的东西
就像这样:
随机(未选中((int)DateTime.Now.Ticks));
但我想要的是什么要知道每次我想在类中生成一个随机数时是否必须创建一个Random
构造函数的实例。
你看我必须生成几个不同类中的随机数,所以
理想情况下,我只想使用一个构造函数,就像你用C ++做的那样(b =(无符号)时间) (NULL))每次获得一个随机数
,然后总是使用相同的实例生成一个随机的
数。这可能吗?
谢谢
Roy
***通过Developersdex发送 http://www.developersdex.com ***
Hi,
How do I invoke the random number generator that was suggested by a few
people. Ideally, what I would like to do is to instantiate the random no.
generator with a seed value that does not repeat the values and that can be
called from any class, as I have to call the random number generator from a
few different classes.
Here is my code:
using System;
class cMain
{
static void Main()
{
}
}
public class cGetRandom
{
static Random rand = new Random();
private static int GetRandom()
{
lock ( rand )
{
return rand.Next();
}
}
}
public class cClass1
{
private static int Class1()
{
int lnRand=0;
lnRand=cGetRandom.rand.Next(1, 49);
}
}
TIA
Roy
Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.
Hi Chris,
Yes I have seen something similar to Random(Environment.TickCount) that is
like this:
Random(unchecked((int)DateTime.Now.Ticks));
but what I want to know is if I have to create an instance of the Random
constructor every time I want to generate a random number in a class. You
see I have to generate random numbers in a few different classes, so ideally
I would have like to just use a constructor only once just as you do in C++
(srand( (unsigned)time( NULL ) ) to get a random number every time and then
always use that same instance to generate a random number. Is that possible?
Thanks
Roy
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.
Hi Chris,
Yes I have seen something similar to Random(Environment.TickCount) that
is like this:
Random(unchecked((int)DateTime.Now.Ticks));
but what I want to know is if I have to create an instance of the Random
constructor every time I want to generate a random number in a class.
You see I have to generate random numbers in a few different classes, so
ideally I would have like to just use a constructor only once just as
you do in C++ (srand( (unsigned)time( NULL ) ) to get a random number
every time and then always use that same instance to generate a random
number. Is that possible?
Thanks
Roy
*** Sent via Developersdex http://www.developersdex.com ***
这篇关于随机无代问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!