C#奇怪的问题!

bat800 2007-05-04 12:29:55
我编了一个程序来随机获得6个不同的数字。(按客户要求获得)
但是程序在执行单步调试时没有任何问题,6个数正确选出。
但是生成可执行文件后,确不能正确选出随机数。出现重复。

程序关键代码如下:
#region 70%的几率5个数出现在前16位,剩下的一位数在 17--21 出现
MyCom mcom = new MyCom();

if(mcom.GetRandom() > mcom.GetNanDu(70,this._GameND))
{
b = 0;
this._Number1 = Get215Rand(16,0);
while(b<18)
{
//获得0到16的随机数
this._Number2 = Get215Rand(16,0);
if(this._Number1==this._Number2)
b++ ;
else
b = 20;
}
b=0;
while(b<18)
{
this._Number3 = Get215Rand(16,0);
if((this._Number3 == this._Number1) || (this._Number3 == this._Number2))
b++;
else
b = 20;
}
b = 0;
while(b<18)
{
this._Number5 = Get215Rand(16,0);
if((this._Number1 == this._Number5) || (this._Number2 == this._Number5) || (this._Number3 == this._Number5))
b ++;
else
b = 20;
}
b = 0;
while(b<18)
{
this._NumberTB = Get215Rand(16,0);
if((this._NumberTB == this._Number5) || (this._NumberTB == this._Number1) || (this._NumberTB == this._Number2) || (this._NumberTB == this._Number3))
b++;
else
b = 20;
}
b = 0;

this._Number4 = Get215Rand(21,17);
}
#endregion
...全文
261 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bat800 2007-05-04
  • 打赏
  • 举报
回复
1到16之间,出现几率70%左右
17到21之间,出现几率30%左右

我上面的问题是不是由于随机数出了问题?
amandag 2007-05-04
  • 打赏
  • 举报
回复
你打算生成的6个随机数有什么规律?在多少之间?
bat800 2007-05-04
  • 打赏
  • 举报
回复
原来不是这样的,现在改成最笨的方法了,但是结果还是有问题
HappinessBoy 2007-05-04
  • 打赏
  • 举报
回复
UP一下~~~
amandag 2007-05-04
  • 打赏
  • 举报
回复
你生成6个不同的随机数的方法效率就很低阿
bat800 2007-05-04
  • 打赏
  • 举报
回复
程序相当简单,就是不知道哪里出了问题,单步调试时跟踪程序没有任何问题。
请教了朋友,他们有时也有这类问题,但是我没有用到多线程,也没有进行浮点运算。
奇怪了!高手指点一二。
wheeler 2007-05-04
  • 打赏
  • 举报
回复
楼上的大大们都不对哦

这个问题的关键是:
现有的随机数获得都是伪随机数
Random ran = new Random();
VS的默认构造函数是以系统时间作为随机数的种子滴,而现在滴PC性能都比较好,所以楼主前后使用的ran.Next()都是用同一个随机数的种子;而楼主单步的时候就不存在这样的问题了。

解决方法:
1、使用Sleep
2、手动使用系统时间的毫秒数作为随机数的种子

PS:这一点VB6.0就做得非常好,完全不存在这种问题,所以可以考虑用VB6.0做个dll也可以
bbdog 2007-05-04
  • 打赏
  • 举报
回复
是不是两台电脑的性能差别太大了啊,要不你在取两次随机数之间加上时间延迟试一下。
中国老陈 2007-05-04
  • 打赏
  • 举报
回复
UP UP UP
bat800 2007-05-04
  • 打赏
  • 举报
回复
继续顶啊
bat800 2007-05-04
  • 打赏
  • 举报
回复
private int Get215Rand(int maxshu,int minshu)
{
Random ran = new Random();
int mshu = 0;
if(minshu == 0)
{
mshu = ran.Next(16);
mshu++;
}
else
{
mshu = ran.Next(5);
switch(mshu)
{
case 0:
mshu = 17;
break;
case 1:
mshu = 18;
break;
case 2:
mshu = 19;
break;
case 3:
mshu = 20;
break;
case 4:
mshu = 21;
break;
default:
mshu = 17;
break;
}
}
return mshu;
}
这个函数出来的结果,都一样,
但是调试的时候,却都不一样!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧