943,594 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 530
  • C++ RSS
May 19th, 2008
0

Random - Different Number that hasnt been drawn.

Expand Post »
My appologies if this has been answered before but I couldnt find a specific answer/response.

Basically Im wondering how I would assure each random number is one that hasnt been produced already. In the program below Im trying to use the random number produced to act as a specific number of an array.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int numb[4], n;
  10. int numb2[4];
  11.  
  12.  
  13. srand((unsigned)time(0));
  14.  
  15. int lowest=0, highest=3;
  16. int range=(highest-lowest)+1;
  17.  
  18. numb2[0] = 10;
  19. numb2[1] = 11;
  20. numb2[2] = 12;
  21. numb2[3] = 13;
  22.  
  23. for ( n=0 ; n<4 ; n++ )
  24. {
  25. numb[n] = lowest+int(range*rand()/(RAND_MAX + 1.0));
  26. cout << numb[n] << endl;
  27. cout << numb2[numb[n]] << endl;
  28. }
  29.  
  30.  
  31.  
  32. system("pause");
  33. }

I tried a few if statements and a while loop to assure that if the number drawn is the same as one already drawn then re-do the random number but it wasnt happening.

Any ideas?

cpp Syntax (Toggle Plain Text)
  1. do
  2. {
  3. numb[2] = lowest+int(range*rand()/(RAND_MAX + 1.0));
  4. }
  5. while ((numb[2] == numb[1]) && (numb[2] == numb[3]) && (numb[2] == numb[0]));

The reason I wish to know how to do this is because for a project I want to randomised the order of how certain strings are shown. I have already used a bubble sort but I was curious for this method.
Safe.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007
May 19th, 2008
0

Re: Random - Different Number that hasnt been drawn.

If you are wanting to contain an array of unique random numbers, then
C++ Syntax (Toggle Plain Text)
  1. for each element in the array
  2. generate a random number
  3. search the array to see if the number already exists
  4. if not, then add it to the array
  5. otherwise, if it was found then go back and generate another number
  6. end of loop

// something like this
C++ Syntax (Toggle Plain Text)
  1. const int maxnums= 10;
  2. int nums[maxnums] = {0};
  3. int curnum = 0;
  4. for(int i = 0; i < maxnums; ++i)
  5. {
  6. bool found = false;
  7. do {
  8. int x = rand();
  9. for(int j = 0; j < curnum && found == false; ++j)
  10. {
  11. if( nums[j] == x)
  12. {
  13. found = true;
  14. }
  15. }
  16. if(found == false)
  17. {
  18. nums[curnum++] = x;
  19. }
  20. } while(found == true);
  21. }
Last edited by Ancient Dragon; May 19th, 2008 at 12:45 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
May 19th, 2008
0

Re: Random - Different Number that hasnt been drawn.

Here is another way to do it
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. const int maxnum = 10;
  10. int nums[maxnum] = {0};
  11. srand((unsigned int)time(0));
  12. int curnum = 0;
  13. for(int i = 0; i < maxnum; ++i)
  14. {
  15. bool found = false;
  16. do
  17. {
  18. int x = rand() % 50;
  19. int* result = std::find(&nums[0], &nums[maxnum-1], x);
  20. if( *result < 0)
  21. {
  22. nums[curnum++] = x;
  23. found = true;
  24. }
  25. } while( found == false);
  26. }
  27. for(int i = 0; i < maxnum; ++i)
  28. cout << nums[i] << "\n";
  29.  
  30. }
Last edited by Ancient Dragon; May 19th, 2008 at 1:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
May 19th, 2008
0

Re: Random - Different Number that hasnt been drawn.

Thanks kindly Ancient Dragon thats exactly what I wanted!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: <map> problems, help pls!
Next Thread in C++ Forum Timeline: How to restrict memory usage of a c++ program





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


Follow us on Twitter


© 2011 DaniWeb® LLC