读书人

无法取随机数,该如何解决

发布时间: 2012-06-15 19:37:05 作者: rapoo

无法取随机数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;
namespace testcsharp
{

class Program
{

public static void Main(string[] args)
{
Random _Rnd = new Random();
for (int i = 0; i < 5;i++ )
{
Console.WriteLine(_Rnd.Next(200));
}
Console.WriteLine("///////////////////////////////////////////");
test t1 = new test();
test t2 = new test();
test t3 = new test();
for (int i = 0; i < 5; i++)
{
t1.Time();
t2.Time();
t3.Time();
}

Console.ReadLine();
}
class test
{
Random m_Rnd = new Random();
public void Time(){
Console.WriteLine(m_Rnd.Next(200));
}
}

}
}
第一个循环能出5个随机数,第二个循环t1,t2,t3出来的数相同。我的目的是要后面那个,也能出来不同的随机数。
大家能帮忙看看吗?



[解决办法]

C# code
class test  {Random rnd=null;public test(Random m_Rnd )  {rnd=m_Rnd;}  public void Time(){  Console.WriteLine(rnd.Next(200));  }  }
[解决办法]
C# code
test t1 = new test();Thread.Sleep(100);test t2 = new test();Thread.Sleep(100);test t3 = new test();
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Cfeibulaqie{    class Program    {        public static void Main(string[] args)        {                       Random _Rnd = new Random();            for (int i = 0; i < 5; i++)            {                Console.WriteLine(_Rnd.Next(200));            }            Console.WriteLine("///////////////////////////////////////////");            test t1 = new test();            Thread.Sleep(300);            test t2 = new test();            Thread.Sleep(300);            test t3 = new test();            for (int i = 0; i < 5; i++)            {                                    t1.Time();                                        t2.Time();                                        t3.Time();                           }            Console.ReadLine();        }        class test        {            Random m_Rnd = new Random();                        public void Time()            {                Console.WriteLine(m_Rnd.Next(200));            }        }    }} 

读书人网 >C#

热点推荐