| | |
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
Views: 413 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






