问题是:生成一个长度为10的字符窜,要求从26个字母中随机选取。
我的代码是:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace 字符串
{
    class Program
    {
        private static int GetInt()
        {
            Random rm = new Random();
            int x = rm.Next(0,27);
            return x;
        }
        static void Main(string[] args)
        {
            ArrayList stral = new ArrayList();
            string all = "abcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < 10; i++)
            {
                int x = GetInt();
                string temp = all.Substring(x, 1);
                stral.Add(temp);
            }
            foreach (string s in stral)
            {
                Console.Write(s);
            }
            Console.Read();
        }
    }
}
可是直接运行就会出现10个一样的字母,逐语句运行就正常,郁闷了  各位大侠指点下啊
我目前没分 希望大家帮个忙

解决方案 »

  1.   

    这个我也碰到过,你把Random的定义放到方法的外面就可以了,
    你可以试下放到类里面去定义~~
      

  2.   

    嘿嘿   我把  对象初始化到类里 用 static Random rm = new Random(); 虽然解决 了  不过还是有点郁闷 不知道原因呢  呵呵
      

  3.   

    Random的问题,要知道程序中Random是通过时间片段来获取随机数的,你的程序如果是一步一步的调试的话,那么你每次的时间间隔很大,当然随机数就不一样了
    但是如果是直接运行,那么时间片段就很小了,所以随机数就一样了,解决的办法,如果是你那样也行,但是不推荐,有种好点的,就是你每次获得两个随机数,然后将这些随机数进行一些运算,可以获得不同的数,怎么算,我也不记得了,以前在网上看到过。