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出来的数相同。我的目的是要后面那个,也能出来不同的随机数。
大家能帮忙看看吗?

解决方案 »

  1.   


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

  2.   

    在构造函数里面 实例化Random 试试。
      

  3.   

    哥们,你的方法可行。但我的那个任务里,Test不能改动。
    能不能通过该MAIN函数来实现?
      

  4.   


    test t1 = new test();
    Thread.Sleep(100);
    test t2 = new test();
    Thread.Sleep(100);
    test t3 = new test();原理
      

  5.   

    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));
                }
            }
        }}
      

  6.   

    有遇到过类似的问题,你在调用方法的时候加个停顿试试 Thread.Sleep(50);