| | |
C++ random between two choices ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Here's how to do it:
Google for srand() and rand() to find out how they work, or read this
C++ Syntax (Toggle Plain Text)
int choices[2] = {2,3}; //choose between 2 and 3 // Seed the random generator with srand() // Use rand() to pick either 0 or 1 and store in variable x int L = choices[x];
Google for srand() and rand() to find out how they work, or read this
or u can do it as
C++ Syntax (Toggle Plain Text)
int random(int a, int b) { srand(time(NULL)); int r = rand()%2; if(r==0) return a; else return b; }
•
•
•
•
thx niek_e, but is there any other way how i to make this with a single line without to use much variables ? Idea is to pick one of these two chars A or B.
Like
char L = random(from char A, or char B);
C++ Syntax (Toggle Plain Text)
char x = random('A', 'B');
C++ Syntax (Toggle Plain Text)
char random(char a, char b);
i hope thats simple enough. i guess thats simplest way to do it.
![]() |
Similar Threads
- help!!! (Python)
- Help! on Basic Mash Python Program (Python)
- Random.randrange & counting (Python)
- randomizing a series of choices (Visual Basic 4 / 5 / 6)
- Python noob needs random help (Python)
- Function that creates random changes in a string?? (Python)
- Random Restarts (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Number of Variables in Constructor
- Next Thread: LINQ with C++
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






