0

C#で繰り返しのない乱数を生成するにはどうすればよいですか。配列が1つあり、すべての部屋に0から9までの乱数を入力したいと思います。各部屋には異なる番号が必要です。私はこれを使用します:

for (int i = 0; i < 20; i++)
{
    Random rnd = new Random();
    int temp = 0;
    temp = rnd.Next(0, 9);
    page[i] = temp;
}

しかし、私はアレイのすべての部屋で同じ数を取得します。

4

7 に答える 7

5

選択できる番号のこのような小さなリストを使用すると、それらすべてを含むリストを生成してから、それらをシャッフルすることができます。

于 2012-05-20T16:34:47.070 に答える
4

あなたの問題は、すべてのループで Random オブジェクトを作成していることです。Random オブジェクトは一度だけ作成する必要があります。代わりにこれを試してください:

Random rnd = new Random(); // <-- This line goes out of the loop        
for (int i = 0; i < 20; i++) {
    int temp = 0;
    temp = rnd.Next(0, 9);
    page[i] = temp;
}
于 2012-12-29T23:52:02.400 に答える
0

これにより、1 から rangeEx までの一意の範囲が作成されます。次の 2 行は、乱数ジェネレーターを作成し、IEnumerable 範囲を乱数で並べ替えます。これは ToArray で呼び出されて返されます!

   private int[] RandomNumber(int rangeEx)
    {
        var orderedList = Enumerable.Range(1, range);
        var rng = new Random();
        return orderedList.OrderBy(c => rng.Next()).ToArray();
    }
于 2016-01-14T17:33:02.800 に答える
-1
using System;

using System.Collections.Generic;

using System.Linq;


namespace nonRepeatableRndm
{

    class Program
    {
        //variable with the Values
        List<string> RandomVal = new List<string>();
        //variable to compare the randomly genarated Values
        List<string> CompaerbyString = new List<string>();
        //Variable that gets Value from  the list Values
        string DisplayVal;
        //instantiates the Random Class
        Random r;
        //this Method gives Value to the list and initializes th the Random Class
        void setVal()
        {
            //Adding to the list
            RandomVal.Add("A");
            RandomVal.Add("b");
            RandomVal.Add("c");
            RandomVal.Add("d");
            RandomVal.Add("e");
            RandomVal.Add("f");
            RandomVal.Add("g");
            RandomVal.Add("h");
            RandomVal.Add("i");
            RandomVal.Add("j");
            RandomVal.Add("k");
            RandomVal.Add("l");
            RandomVal.Add("m");
            RandomVal.Add("n");
            RandomVal.Add("o");
            RandomVal.Add("p");
            RandomVal.Add("q");
            RandomVal.Add("r");
            RandomVal.Add("s");
            RandomVal.Add("t");
            RandomVal.Add("u");
            RandomVal.Add("v");
            RandomVal.Add("w");
            RandomVal.Add("x");
            RandomVal.Add("y");
            RandomVal.Add("z");

            //Instantiating the Random Method
            r = new Random();
        }
        //This method Gives Out the Random Values
        public void DisplayRand()
        {

           //Setting Random Index 
           int getIndex =  r.Next(0, RandomVal.Count - 1);
            //Now we are trying to pass a random value to the String 
            DisplayVal = RandomVal.ElementAt<string>(getIndex);
            //we are testing to see if String in Display is contained in the List that will used Compare
            if (!CompaerbyString.Contains(DisplayVal))
                Console.WriteLine(DisplayVal.ToUpper());
            else
            {
                try
                {
                    this.DisplayRand();
                }
                catch(Exception e)
                {
                    Console.WriteLine("You have Reached the End of the list...");
                    Environment.Exit(0);
                }
            }
            //Adding Corrent DisplayVal's Value to the List for Comparison
            CompaerbyString.Add(DisplayVal);
        }
        //This is Simple method that Calls the Display
        void Call()
        {
            //This For loop is to Ensure we have no Stack Overflow
            for ( int i = 0; i < RandomVal.Count-1;i++)
            {
                this.DisplayRand();

            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Random Values With Out Repeatating Any Value");
            //Simple Instantiation
            Program dis = new Program();
            //Simple Call
            dis.setVal();
            //Simple Call
            dis.Call() ;
            Console.ReadLine();   
        }
    }
}

これが出力です

于 2016-01-14T16:22:04.427 に答える
-2

100% 正しい

   public int[] UniqeRandomArray(int size , int Min , int Max ) {

        int [] UniqueArray = new int[size];
        Random rnd = new Random();
        int Random;

        for (int i = 0 ; i < size ; i++) {

            Random = rnd.Next(Min, Max);

            for (int j = i; j >= 0 ; j--) {

                if (UniqueArray[j] == Random)
                { Random = rnd.Next(Min, Max); j = i; }

            }

            UniqueArray[i] = Random;

        }

        return UniqueArray;

    }

// 一意であることを通知 [最大 - 最小 > サイズ] 等しくない

于 2013-12-01T08:07:49.470 に答える