Filling an array randomly with no repetitions [duplicate]

假装没事ソ 提交于 2019-12-13 04:08:54

问题


I'm trying to fill an array with random numbers from 1-10 with no repeat. I try to do it with recursion. I'm trying to it with recursion and without (here is both with, no luck in either way). I have two codes, boths not working:

1:

static int reco(int arr,int[] times)
{
    Random rnd = new Random();
    arr = rnd.Next(1, 11);
    return times[arr] > 0 ? reco(arr, times) : arr;
}

static void Main(string[] args)
{
    int i = 0;
    int[] arr = new int[10];
    int[] times = new int[11];
    Random rnd = new Random();

    for (i = 0; i < 10; i++)
    {
        arr[i] = rnd.Next(1, 11);
        times[arr[i]]++;

        if (times[arr[i]] > 0)
            arr[i] = reco(arr[i], times);
    }

2:

static int reco(int arr,int[] times)
{
    Random rnd = new Random();
    arr = rnd.Next(1, 11);
    if (times[arr] > 0)
        return reco(arr, times);
    else
        return arr;
}
static void Main(string[] args)
{
    int i = 0;
    int[] arr = new int[10];
    int[] times = new int[11];
    Random rnd = new Random();
    for (i = 0; i < 10; i++)
    {
        arr[i] = rnd.Next(1, 11);

        if (times[arr[i]] > 0)
            arr[i] = reco(arr[i], times);

        times[arr[i]]++;
    }
}

回答1:


If you just want random numbers between 1 and 10, you could just use Enumerable.Range and order randomly.

var ran = new Random();
int[] randomArray = Enumerable.Range(1, 10).OrderBy(x => ran.Next()).ToArray();



回答2:


Generate unique "random" numbers within a specific range like:

List<int> theList = Enumerable.Range(0, 10).ToList();
theList.Shuffle();

Example output:

[1,5,4,8,2,9,6,3,7,0]

Shuffle function (source: Randomize a List<T>):

public static void Shuffle<T>(this IList<T> list)  
{  
    Random rng = new Random();  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}



回答3:


Since you're using C#, and you know the random numbers in the array, why not just create an array, then randomize the positions? Here is an example:

using System.Linq;

//......

Random rand = new Random();
int[] randomNumbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
randomNumbers.OrderBy(num => rand.Next());



回答4:


    static void Main()
    {
        int[] arr = new int[10];
        List<int> numbers = Enumerable.Range(1, 10).ToList();
        Random rnd = new Random();
        for (int i = 0; i < 10; i++)
        {
            int index = rnd.Next(0, numbers.Count - 1);
            arr[i] = numbers[index];
            numbers.RemoveAt(index);
        }
    }



回答5:


You could do this. The recursive version is left as an exercise for the original poster because the question sounds like a homework exercise:

int[] arr = new int[10];

// Fill the array with values 1 to 10:
for (int i = 0; i < arr.Length; i++)
{
    arr[i] = i + 1;
}

// Switch pairs of values for unbiased uniform random distribution:
Random rnd = new Random();
for (int i = 0; i < arr.Length - 1; i++)
{
    int j = rnd.Next(i, arr.Length);

    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

This uses the Fisher-Yates (Knuth) shuffle as proposed by Eric Lippert in the comments below.



来源:https://stackoverflow.com/questions/14884934/filling-an-array-randomly-with-no-repetitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!