| | |
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 beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






