Hello friends,
Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.
The code i have developed is below
Hope replies asap from you friends.Code:#include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> int random () { srand (time (NULL)); int picked[8], i; for (i = 0; i < 9; i++) picked[i] = 0; int array[9]; int value; for (i = 0; i < 9; i++) { value = rand () % 9; if (picked[value]) i--; // already picked. for-loop increments, so decrement here else { array[i] = value; picked[value] = 1; // hasn't been picked yet. Assign to array, // flag as picked. } } // display for (i = 0; i < 9; i++) printf("Values in the array are %d\n", array[i]); getch(); return value; } int main() { int s; s = random(); printf("value=%d", s); getch(); }



LinkBack URL
About LinkBacks


