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
0
Newbie Poster

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.

Jump to PostHere'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 …

Jump to Postor 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; }`

Jump to Postif random is used like random(high number)

why not do random(number)+15sorry but i didn't get your question. Can u put some light on what u wanted to know.

Jump to Postso 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.

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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

dkalita
commented:
good enough
+1

dkalita
110
Posting Pro in Training

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

maxicube
0
Light Poster

if random is used like random(high number)

why not do random(number)+15

dkalita
110
Posting Pro in Training

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.

maxicube
0
Light Poster

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

dkalita
110
Posting Pro in Training

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.

johndoe123
0
Newbie Poster

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

dkalita
110
Posting Pro in Training

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.

dunnotheclown
0
Newbie Poster

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

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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

dkalita
110
Posting Pro in Training

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

mrnutty
761
Senior Poster

`char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }`

Tom Gunn
1,164
Practically a Master Poster

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

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.