Saturday, 15 May 2010

c# - Filling an array randomly with no repetitions -



c# - Filling an array randomly with no repetitions -

this question has reply here:

generate distinct random numbers in c# 8 answers

i'm trying fill array random numbers 1-10 no repeat. seek recursion. i'm trying recursion , without (here both with, no luck in either way). have 2 codes, boths not working:

1:

static int reco(int arr,int[] times) { random rnd = new random(); arr = rnd.next(1, 11); homecoming times[arr] > 0 ? reco(arr, times) : arr; } static void main(string[] args) { int = 0; int[] arr = new int[10]; int[] times = new int[11]; random rnd = new random(); (i = 0; < 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) homecoming reco(arr, times); else homecoming arr; } static void main(string[] args) { int = 0; int[] arr = new int[10]; int[] times = new int[11]; random rnd = new random(); (i = 0; < 10; i++) { arr[i] = rnd.next(1, 11); if (times[arr[i]] > 0) arr[i] = reco(arr[i], times); times[arr[i]]++; } }

if want random numbers between 1 , 10, utilize enumerable.range , order randomly.

var ran = new random(); int[] randomarray = enumerable.range(1, 10).orderby(x => ran.next()).toarray();

c# recursion repeat

No comments:

Post a Comment