954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ random between two choices ?

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.

johndoe123
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Here's how to do it:

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

or u can do it as

int random(int a, int b)
{
      srand(time(NULL));
   
      int r = rand()%2;

      if(r==0)
            return a;
      else
            return b;
}
dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

if random is used like random(high number)
why not do random(number)+15

maxicube
Light Poster
33 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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.

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

so if 0 is randoms starting number, you add 15 so... 15 is the minimum number

maxicube
Light Poster
33 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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.

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

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);

johndoe123
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

char x = random('A', 'B');

just change its prototype to

char random(char a, char b);


i hope thats simple enough. i guess thats simplest way to do it.

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

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

dunnotheclown
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 0
 
srand(time(NULL)); // put this at the beginning of your program
//do stuff
char choice[2] = {'a','b'};
char L = choice[rand()%2];
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
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


hey but this is a C++ forum............

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 
char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }

rand()%2 might not be very random. Historically rand() has been written with very non-random low order bits, and using modulus to fit the range uses the low order bits. rand()%2 only uses one bit, so you might end up with a predictable pattern like 1,0,1,01,0,1,0... A better way to pick between two options is to divide RAND_MAX by 2 and look for values in that range:

template <typename T>
T RandomChoice(T a, T b)
{
    return (rand() < RAND_MAX / 2) ? a : b;
}


p.s. I am not picking on you firstPerson. Other examples in this thread used the same thing, so my post applies to them too.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }

Or another step higher in obfuscation:

char AorB(char A, char B) { return (rand()%2 ?  A : B); }
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You