943,645 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2118
  • C RSS
Feb 5th, 2009
0

Random number generation from an array without repeats

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kishore84 is offline Offline
12 posts
since Sep 2008
Feb 6th, 2009
0

Re: Random number generation from an array without repeats

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 6th, 2009
0

Re: Random number generation from an array without repeats

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Feb 7th, 2009
1

Re: Random number generation from an array without repeats

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Function to grab User/Password combinations from a file
Next Thread in C Forum Timeline: word count problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC