0

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.

7
Contributors
14
Replies
16
Views
8 Years
Discussion Span
Last Post by Nick Evan
Featured Replies
  • 1

    Here's how to do it: [code=cplusplus] 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]; [/code] Google for srand() and rand() to find out how … Read More

1

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

Votes + Comments
good enough
0

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;
}
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.

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.

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

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.

0

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

0
srand(time(NULL)); // put this at the beginning of your program
//do stuff
char choice[2] = {'a','b'};
char L = choice[rand()%2];
0

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............

0
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.

0
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); }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.