| | |
Random number generation from an array without repeats
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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
C Syntax (Toggle Plain Text)
#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(); }
Hope replies asap from you friends.
This code: won't work unless you defined picked with enough space for all the random integers possible (RAND_MAX). This of course is not a great idea because you would waste an awful lot of space. Try setting each array element to the random number generated. To check if it has already been picked, loop through the array and see if there are any matches to the number generated.
C Syntax (Toggle Plain Text)
picked[value] = 1; // hasn't been picked yet. Assign to array, // flag as picked.
Last edited by death_oclock; Feb 6th, 2009 at 5:52 pm.
Apart from the heavy memory consumption problem stated by "death_o_clock" which need to be corrected,your program has other major errors.
1> picked[8] can carry flags only up to integer 7 and
will itself give error.Therefore picked[9] is the declaration you should make for your idea to work.
2> Other major flaw is in code
You have taken so much time to generate 9 random numbers 0-8 ( rand() % 9 )such that if a number repeats then it is not considered and again a number is generated a random.
After all this effort you are just sending the random number which is generated at last back to main.
This is just as calling :
in main itself.Ultimately you are just getting the random value generated on the ninth looping.
1> picked[8] can carry flags only up to integer 7 and
c Syntax (Toggle Plain Text)
for (i = 0; i < 9; i++) picked[i] = 0;
2> Other major flaw is in code
c Syntax (Toggle Plain Text)
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;
After all this effort you are just sending the random number which is generated at last back to main.
This is just as calling :
c Syntax (Toggle Plain Text)
for(i=0;i<9;i++) s=rand()%9;
I Surf in "C"....
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
This question has been asked many times. There's a few ways to do this, an easy way is to simply fill up the array with unique integers, then shuffle it like this: Hope this helps.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { int arr[10]; int i; for (i = 0; i < 10; ++i) arr[i] = i; for (i = 0; i < 10; ++i) { int *a = &arr[ i ]; int *b = &arr[ rand() % 10 ]; int temp = *a; *a = *b; *b = temp; } for (i = 0; i < 10; ++i) printf( "%d ", arr[i] ); }
Last edited by William Hemsworth; Feb 7th, 2009 at 10:10 am.
![]() |
Similar Threads
- Shuffling (C++)
Other Threads in the C Forum
- Previous Thread: Function to grab User/Password combinations from a file
- Next Thread: word count problem
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






