Random - Different Number that hasnt been drawn.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 57
Reputation: Run.[it] is an unknown quantity at this point 
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Random - Different Number that hasnt been drawn.

 
0
  #1
May 19th, 2008
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.

  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?

  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.
.........scaricamento.........
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Random - Different Number that hasnt been drawn.

 
0
  #2
May 19th, 2008
If you are wanting to contain an array of unique random numbers, then
  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
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Random - Different Number that hasnt been drawn.

 
0
  #3
May 19th, 2008
Here is another way to do it
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 57
Reputation: Run.[it] is an unknown quantity at this point 
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Re: Random - Different Number that hasnt been drawn.

 
0
  #4
May 19th, 2008
Thanks kindly Ancient Dragon thats exactly what I wanted!
.........scaricamento.........
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



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