| | |
Random - Different Number that hasnt been drawn.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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?
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.
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)
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int numb[4], n; int numb2[4]; srand((unsigned)time(0)); int lowest=0, highest=3; int range=(highest-lowest)+1; numb2[0] = 10; numb2[1] = 11; numb2[2] = 12; numb2[3] = 13; for ( n=0 ; n<4 ; n++ ) { numb[n] = lowest+int(range*rand()/(RAND_MAX + 1.0)); cout << numb[n] << endl; cout << numb2[numb[n]] << endl; } system("pause"); }
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)
do { numb[2] = lowest+int(range*rand()/(RAND_MAX + 1.0)); } 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.........
If you are wanting to contain an array of unique random numbers, then
// something like this
C++ Syntax (Toggle Plain Text)
for each element in the array generate a random number search the array to see if the number already exists if not, then add it to the array otherwise, if it was found then go back and generate another number end of loop
// something like this
C++ Syntax (Toggle Plain Text)
const int maxnums= 10; int nums[maxnums] = {0}; int curnum = 0; for(int i = 0; i < maxnums; ++i) { bool found = false; do { int x = rand(); for(int j = 0; j < curnum && found == false; ++j) { if( nums[j] == x) { found = true; } } if(found == false) { nums[curnum++] = x; } } while(found == true); }
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.
Here is another way to do it
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <algorithm> #include <ctime> using namespace std; int main() { const int maxnum = 10; int nums[maxnum] = {0}; srand((unsigned int)time(0)); int curnum = 0; for(int i = 0; i < maxnum; ++i) { bool found = false; do { int x = rand() % 50; int* result = std::find(&nums[0], &nums[maxnum-1], x); if( *result < 0) { nums[curnum++] = x; found = true; } } while( found == false); } for(int i = 0; i < maxnum; ++i) cout << nums[i] << "\n"; }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: <map> problems, help pls!
- Next Thread: How to restrict memory usage of a c++ program
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






