C++ random between two choices ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 2
Reputation: johndoe123 is an unknown quantity at this point 
Solved Threads: 0
johndoe123 johndoe123 is offline Offline
Newbie Poster

C++ random between two choices ?

 
0
  #1
Sep 24th, 2009
Can some help me with a random between two choices ?

Something like:

L = random(A, B);

and in L to have A or B.

Thx.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,859
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: C++ random between two choices ?

 
1
  #2
Sep 24th, 2009
Here's how to do it:

  1. int choices[2] = {2,3}; //choose between 2 and 3
  2. // Seed the random generator with srand()
  3. // Use rand() to pick either 0 or 1 and store in variable x
  4. int L = choices[x];

Google for srand() and rand() to find out how they work, or read this
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 345
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 51
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: C++ random between two choices ?

 
0
  #3
Sep 24th, 2009
or u can do it as

  1. int random(int a, int b)
  2. {
  3. srand(time(NULL));
  4.  
  5. int r = rand()%2;
  6.  
  7. if(r==0)
  8. return a;
  9. else
  10. return b;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: maxicube is an unknown quantity at this point 
Solved Threads: 0
maxicube maxicube is offline Offline
Newbie Poster

Re: C++ random between two choices ?

 
0
  #4
Sep 24th, 2009
if random is used like random(high number)
why not do random(number)+15
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 345
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 51
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: C++ random between two choices ?

 
0
  #5
Sep 24th, 2009
Originally Posted by maxicube View Post
if random is used like random(high number)
why not do random(number)+15
sorry but i didn't get your question. Can u put some light on what u wanted to know.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: maxicube is an unknown quantity at this point 
Solved Threads: 0
maxicube maxicube is offline Offline
Newbie Poster

Re: C++ random between two choices ?

 
0
  #6
Sep 24th, 2009
so if 0 is randoms starting number, you add 15 so... 15 is the minimum number
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 345
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 51
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: C++ random between two choices ?

 
0
  #7
Sep 24th, 2009
Originally Posted by maxicube View Post
so if 0 is randoms starting number, you add 15 so... 15 is the minimum number
offcourse..........

u can manipulate the random number in whatever way u want it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: johndoe123 is an unknown quantity at this point 
Solved Threads: 0
johndoe123 johndoe123 is offline Offline
Newbie Poster

Re: C++ random between two choices ?

 
0
  #8
Sep 24th, 2009
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);
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 345
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 51
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: C++ random between two choices ?

 
0
  #9
Sep 24th, 2009
Originally Posted by johndoe123 View Post
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);
u can use the function that i gave as

  1. char x = random('A', 'B');
just change its prototype to
  1. char random(char a, char b);

i hope thats simple enough. i guess thats simplest way to do it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: dunnotheclown is an unknown quantity at this point 
Solved Threads: 0
dunnotheclown dunnotheclown is offline Offline
Newbie Poster

Re: C++ random between two choices ?

 
0
  #10
Sep 24th, 2009
hi there i'm new to this and I have a problem with dreamweaver and don't know who to ask.
when I try to view my pages with hitting F12 it keeps saying that I have a broken link and a DNS error.
can you point me in the right direction?
Thank you in advance
Robert
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC