随机问题() [英] Problem with Random()
问题描述
如果我在我的页面中使用它就没问题但是当我把它放在一个类文件中时,
调用它会为每次调用返回相同的#。
任何想法为什么?我确定这是我会拍的,但是我能找到的唯一的
样本是用来调用页面内的Random(),而不是
单独的课程。
--Page--
myClass myclass = new myClass();
for(int i = 1; i <= 100; i ++)
{
this.myLabel.Text + = i +" - + myClass.GetRandom()+"< br>" ;;
}
----类文件
公共字符串GetRandom()
{
随机rnd = new Random();
return rnd.Next(1,100));
}
-
Curt Christianson
所有者/首席开发人员,DF-Software
网站: http://www.Darkfalz.com
博客: http://blog.Darkfalz.com
If I use it in my page it''s fine but when I put it in a Class file for
calling it returns the same # for each call.
Any ideas why? I''m sure it''s something I''ll slap myself for but the only
samples I can find are for calling the Random() within the page, not a
seperate class.
--Page--
myClass myclass = new myClass();
for(int i = 1;i <= 100; i++)
{
this.myLabel.Text += i + " - " + myClass.GetRandom() + "<br>";
}
---- class file
public string GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
推荐答案
随机数生成从种子值开始。如果重复使用相同的种子
,则会生成相同的数字序列。生成
不同序列的一种方法是使种子值与时间相关,从而为每个新的Random实例生成不同的系列。
为了提高性能,创建一个随机数以随着时间的推移生成许多随机数
,而不是重复创建一个新的随机数以生成一个
随机数。
从msdn中提取。时间依赖种子。
-
问候,
隐士戴夫
( http://hdave.blogspot.com )
" ; Curt_C [MVP]" < software_AT_darkfalz.com>在消息中写道
news:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
The random number generation starts from a seed value. If the same seed is
used repeatedly, the same series of numbers is generated. One way to produce
different sequences is to make the seed value time-dependent, thereby
producing a different series with each new instance of Random.
To improve performance, create one Random to generate many random numbers
over time, instead of repeatedly creating a new Random to generate one
random number.
extract from msdn. Time Dependant seed.
--
Regards,
Hermit Dave
(http://hdave.blogspot.com)
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
如果我在我的页面很好但是当我把它放在一个类文件中进行调用时,它会为每个调用返回相同的#。
任何想法为什么?我确定这是我要打的一些东西,但我能找到的唯一
样本是在页面内调用Random(),而不是单独的类。
--Page--
myClass myclass = new myClass();
for(int i = 1; i< = 100; i ++)
{
this.myLabel.Text + = i +" - + myClass.GetRandom()+"< br>" ;;
}
----类文件
公共字符串GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
-
Curt Christianson
拥有者/首席开发人员,DF-软件
网站: http://www.Darkfalz.com
博客: http://blog.Darkfalz.com >
If I use it in my page it''s fine but when I put it in a Class file for
calling it returns the same # for each call.
Any ideas why? I''m sure it''s something I''ll slap myself for but the only
samples I can find are for calling the Random() within the page, not a
seperate class.
--Page--
myClass myclass = new myClass();
for(int i = 1;i <= 100; i++)
{
this.myLabel.Text += i + " - " + myClass.GetRandom() + "<br>";
}
---- class file
public string GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
我曾尝试使用种子System.DateTime.Now.Millisecond但我的
数字必须生成太快那工作。必须需要纳秒我
猜........
-
Curt Christianson
所有者/首席开发人员,DF-Software
网站: http:/ /www.Darkfalz.com
博客: http:// blog.Darkfalz.com
" Hermit Dave" <他************ @ CAPS.AND.DOTS.hotmail.com>写在消息
news:uv ************** @ TK2MSFTNGP15.phx.gbl ...
I had tried with the seed being System.DateTime.Now.Millisecond but my
numbers must generate too fast for that to work. Must need nanosecond I
guess........
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
随机数生成开始来自种子价值。如果重复使用相同的种子,则会生成相同的数字序列。产生不同序列的一种方法是使种子值与时间相关,从而与每个新的Random实例产生不同的系列。
改进性能,创建一个随机生成许多随机数随着时间的推移,而不是重复创建一个新的随机生成一个
随机数。
从msdn中提取。时间依赖的种子。
-
问候,
隐士戴夫
( http://hdave.blogspot.com )
Curt_C [MVP]" < software_AT_darkfalz.com>在消息中写道
新闻:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
The random number generation starts from a seed value. If the same seed is
used repeatedly, the same series of numbers is generated. One way to
produce
different sequences is to make the seed value time-dependent, thereby
producing a different series with each new instance of Random.
To improve performance, create one Random to generate many random numbers
over time, instead of repeatedly creating a new Random to generate one
random number.
extract from msdn. Time Dependant seed.
--
Regards,
Hermit Dave
(http://hdave.blogspot.com)
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
如果我在我的页面中使用它'没关系,但是当我把它放在一个类文件中进行调用时,它会为每个调用返回相同的#。
任何想法为什么?我确定这是我要打的一些东西,但我能找到的唯一
样本是在页面内调用Random(),而不是单独的类。
--Page--
myClass myclass = new myClass();
for(int i = 1; i< = 100; i ++)
{
this.myLabel.Text + = i +" - + myClass.GetRandom()+"< br>" ;;
}
----类文件
公共字符串GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
-
Curt Christianson
拥有者/首席开发人员,DF-软件
网站: http://www.Darkfalz.com
博客: http://blog.Darkfalz.com >
If I use it in my page it''s fine but when I put it in a Class file for
calling it returns the same # for each call.
Any ideas why? I''m sure it''s something I''ll slap myself for but the only
samples I can find are for calling the Random() within the page, not a
seperate class.
--Page--
myClass myclass = new myClass();
for(int i = 1;i <= 100; i++)
{
this.myLabel.Text += i + " - " + myClass.GetRandom() + "<br>";
}
---- class file
public string GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
我知道它仍然无法解释
如果我在我的页面中使用它没问题,但是当我把它放在一个Class>文件中为
调用时,它会为每次调用返回相同的#。
如果我找到任何东西,我确实环顾四周我将收到一条消息
-
问候,
Hermit Dave
( http:// h dave.blogspot.com )
" Hermit Dave" <他************ @ CAPS.AND.DOTS.hotmail.com>在消息中写道
news:uv ************** @ TK2MSFTNGP15.phx.gbl ...随机数生成从种子值开始。如果重复使用相同的种子,则会生成相同的数字序列。
生成不同序列的一种方法是使种子值与时间相关,从而为每个新的Random实例生成不同的序列。
为了提高性能,创建一个随机生成许多随机数随着时间的推移,而不是重复创建一个新的Random来生成一个
随机数。
从msdn中提取。时间依赖的种子。
-
问候,
隐士戴夫
( http://hdave.blogspot.com )
Curt_C [MVP]" < software_AT_darkfalz.com>在消息中写道
新闻:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
If I use it in my page it''s fine but when I put it in a Class >file for calling it returns the same # for each call.
I did look around if i find anything i will drop in a message
--
Regards,
Hermit Dave
(http://hdave.blogspot.com)
"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl... The random number generation starts from a seed value. If the same seed is
used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby
producing a different series with each new instance of Random.
To improve performance, create one Random to generate many random numbers
over time, instead of repeatedly creating a new Random to generate one
random number.
extract from msdn. Time Dependant seed.
--
Regards,
Hermit Dave
(http://hdave.blogspot.com)
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
如果我在我的页面中使用它'没关系,但是当我把它放在一个类文件中进行调用时,它会为每个调用返回相同的#。
任何想法为什么?我确定这是我要打的一些东西,但我能找到的唯一
样本是在页面内调用Random(),而不是单独的类。
--Page--
myClass myclass = new myClass();
for(int i = 1; i< = 100; i ++)
{
this.myLabel.Text + = i +" - + myClass.GetRandom()+"< br>" ;;
}
----类文件
公共字符串GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
-
Curt Christianson
拥有者/首席开发人员,DF-软件
网站: http://www.Darkfalz.com
博客: http://blog.Darkfalz.com >
If I use it in my page it''s fine but when I put it in a Class file for
calling it returns the same # for each call.
Any ideas why? I''m sure it''s something I''ll slap myself for but the only
samples I can find are for calling the Random() within the page, not a
seperate class.
--Page--
myClass myclass = new myClass();
for(int i = 1;i <= 100; i++)
{
this.myLabel.Text += i + " - " + myClass.GetRandom() + "<br>";
}
---- class file
public string GetRandom()
{
Random rnd = new Random();
return rnd.Next(1,100));
}
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
这篇关于随机问题()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!