| | |
C++ random between two choices ?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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++
Views: 649 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






