Random number generation from an array without repeats

Reply

Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Random number generation from an array without repeats

 
0
  #1
Feb 5th, 2009
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

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. int random ()
  7. {
  8. srand (time (NULL));
  9.  
  10. int picked[8], i;
  11. for (i = 0; i < 9; i++)
  12. picked[i] = 0;
  13.  
  14. int array[9];
  15. int value;
  16.  
  17. for (i = 0; i < 9; i++)
  18. {
  19. value = rand () % 9;
  20. if (picked[value])
  21. i--; // already picked. for-loop increments, so decrement here
  22. else
  23. {
  24. array[i] = value;
  25. picked[value] = 1; // hasn't been picked yet. Assign to array,
  26. // flag as picked.
  27. }
  28. }
  29.  
  30. // display
  31. for (i = 0; i < 9; i++)
  32. printf("Values in the array are %d\n", array[i]);
  33. getch();
  34. return value;
  35. }
  36. int main()
  37. {
  38. int s;
  39. s = random();
  40.  
  41. printf("value=%d", s);
  42. getch();
  43.  
  44. }

Hope replies asap from you friends.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Random number generation from an array without repeats

 
0
  #2
Feb 6th, 2009
This code:
  1. picked[value] = 1; // hasn't been picked yet. Assign to array,
  2. // flag as picked.
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.
Last edited by death_oclock; Feb 6th, 2009 at 5:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Random number generation from an array without repeats

 
0
  #3
Feb 6th, 2009
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
  1. for (i = 0; i < 9; i++)
  2. picked[i] = 0;
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
  1. for (i = 0; i < 9; i++)
  2. {
  3. value = rand () % 9;
  4. if (picked[value])
  5. i--; // already picked. for-loop increments, so decrement here
  6. else
  7. {
  8. array[i] = value;
  9. picked[value] = 1; // hasn't been picked yet. Assign to array,
  10. // flag as picked.
  11. }
  12. }
  13.  
  14. // display
  15. for (i = 0; i < 9; i++)
  16. printf("Values in the array are %d\n", array[i]);
  17. getch();
  18. return value;
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 :
  1. for(i=0;i<9;i++)
  2. s=rand()%9;
in main itself.Ultimately you are just getting the random value generated on the ninth looping.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,427
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Random number generation from an array without repeats

 
1
  #4
Feb 7th, 2009
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:
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int arr[10];
  5.  
  6. int i;
  7. for (i = 0; i < 10; ++i)
  8. arr[i] = i;
  9.  
  10. for (i = 0; i < 10; ++i) {
  11. int *a = &arr[ i ];
  12. int *b = &arr[ rand() % 10 ];
  13.  
  14. int temp = *a;
  15. *a = *b;
  16. *b = temp;
  17. }
  18.  
  19. for (i = 0; i < 10; ++i)
  20. printf( "%d ", arr[i] );
  21. }
Hope this helps.
Last edited by William Hemsworth; Feb 7th, 2009 at 10:10 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC