日期:2014-05-18  浏览次数:20940 次

如何获得一组不相同的随机数

如何获得一组不相同的随机数

int[] nums = new int[200];
Threading.Sleep(1);
Random random = new Random();
for (int i=0; i < nums.Length; i++)
{
  for (int j=1; j <= i; j++)
  if (nums[i]=nums[j]) { i--; break;}
  nums[i]=random.Next(0,200);
}
   
我大概就知道这么多,求个高效的算法

------解决方案--------------------
C# code
        int[] numbers = new int[100];
        for (int i = 0; i < 100; i++)
            numbers[i] = i;
        Random rd = new Random();
        int index, realLength = numbers.Length, temp;
        for (int i = 0; i < 100; i++)
        {
            index = rd.Next(realLength);
            Response.Write(numbers[index] + "<br/>");
            temp = numbers[index];
            numbers[index] = numbers[realLength - 1];
            numbers[realLength - 1] = temp;
            realLength--;
        }

------解决方案--------------------
C# code
        List<int> numbers = new List<int>();
        for (int i = 0; i < 100; i++)
            numbers.Add(i);
        Random rd = new Random();
        int index;
        for (int i = 0; i < 100; i++)
        {
            index = rd.Next(numbers.Count);
            Response.Write(numbers[index] + "<br/>");
            numbers.RemoveAt(index);
        }

------解决方案--------------------
test
C# code
            int[] result = new int[120];
            Random ran = new Random();
            int left = 1900, right = 2020;
            bool[] flag = new bool[right];
            for (int i = 0; i < result.Length; i++)
            {
                int temp = ran.Next(left, right);
                left = (temp + 1) == left ? temp + 1 : left;
                right = (temp - 1) == right ? right - 1 : right;
                while (flag[temp])
                {
                    temp = ran.Next(left, right);
                }
                flag[temp] = true;
                result[i] = temp;
            }
            foreach (int i in result)
                Console.WriteLine(i);

------解决方案--------------------
C# code

class Program {
    static void Main(string[] args) {
        Console.WriteLine(string.Join(",", new Poker(1900, 2020).Shuffle().ToArray()));

        Console.WriteLine("press any key to exit.");
        Console.ReadLine();
    }
}

public class Poker : List<int> {
    public Poker(int begin, int end) {
        if (end <= begin) throw new ArgumentException();

        this.begin = begin;
        this.end = end;
        random = new Random((int)DateTime.Now.Ticks);
    }

    public Poker Shuffle() {
        List<int> source = new List<int>();
        for (int i = begin; i <= end; ++i) { source.Add(i); }
        int? next = null;
        while ((source.Count > 0) && ((next = Pop(source)) != null)) {
            this.Add(source[next.Value]);
            source.RemoveAt(next.Value);
        }
        return this;
    }

    private int? Pop(List<int> source) {
        return source.Count > 1 ? random.Next(source.Count) : 0;
    }

    private Random random;
    private int begin, end;
}