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]]++;
}
}