随机值没有重复

时间:2016-04-14 16:38:44

标签: c# random int duplicates

Random r = new Random();
int randomvalue = r.Next(1,20);
*
*
if(randomvalue == 1) something
if(randomvalue == 2) something
*
*
if(randomvalue == 19) something

使randomvalue不重复的最佳方法是什么?顺便说一句:它的WPF,而不是控制台。

6 个答案:

答案 0 :(得分:1)

尝试以下内容:

随机randomInstance = new Random();

        List<int> NumList = new List<int>();
        NumList.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7,
            8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
            22, 23, 24, 25, 26, 27, 28, 29, 30 });

        int index = randomInstance.Next(0, NumList.Count - 1);
        int randomNumber = NumList[index];
        NumList.RemoveAt(index);

答案 1 :(得分:0)

//这将有效

课程计划     {         static void Main(string [] args)         {

        List<int> i = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20});
        List<int> r;

        r = ShuffleList(i);


    }


    private static List<E> ShuffleList<E>(List<E> unshuffledList)
    {
        List<E> sList = new List<E>();

        Random r = new Random();
        int randomIndex = 0;
        while (unshuffledList.Count > 0)
        {
            randomIndex = r.Next(0, unshuffledList.Count); 
            sList.Add(unshuffledList[randomIndex]);
            unshuffledList.RemoveAt(randomIndex); //remove so wont be added a second time
        }

        return sList; //return the new shuffled list
    }
}

答案 2 :(得分:0)

试试这个:

var rnd = new Random();

var shuffled =
    Enumerable
        .Range(1, 20)
        .OrderBy(x => rnd.Next())
        .ToArray();

答案 3 :(得分:-1)

重复并不清楚您的意思。如果您不希望连续看到相同的数字,那么只需保留一个哈希表或Dictionary<int,int>,它会保留最后整数。然后检查下一个号码是否与最后一个号码相同。如果没有,从字典中删除最后一个数字并放入当前数字。如果它们相同,则从Random请求另一个随机整数。

var myDictionary = new Dictionary<int,int>;
var randomvalue = r.Next(1,20);
while(myDictionary.ContainsKey(randomvalue))
{
   randomvalue = r.Next(1,20);
}
myDictionary.Clear();
myDictionary.Add(randomvalue, 123); //123 is just a number. Doesn't matter.

这可以保证两个相同的整数永远不会连续出现。

注意::其他答案很有创意,但&#34;知道您的数据结构&#34;。请勿使用查找列表。哈希就是我们用来做它的。

答案 4 :(得分:-1)

您需要存储随机值是使其唯一的唯一方法。

试试这个:

Random r = new Random(); 
public List<int> randomList = new List<int>();
int randomvalue = 0;
Public void newNumber()
{
    randomvalue = r.Next(0, 20);

    if (!randomList.Contains(randomvalue))
    {
        randomList.Add(randomvalue);
        if(randomvalue == 1) 
          //do something
        if(randomvalue == N)
          // do something
    }
}

答案 5 :(得分:-1)

using System;
using System.Collections;
using System.Collections.Generic;

namespace SOFAcrobatics
{
    public static class Launcher
    {
        public static void Main ()
        {
            // 1 to 20 without duplicates
            List<Int32> ns = new List<Int32>();
            Random r = new Random();
            Int32 ph = 0;
            while (ns.Count < 20)
            {
                while (ns.Contains (ph = r.Next(1, 21))) {}
                ns.Add(ph);
            }
            // ns is now populated with random unique numbers (1 to 20)
        }
    }
}