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.
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.
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
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;
}
if random is used like random(high number)
why not do random(number)+15
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.
so if 0 is randoms starting number, you add 15 so... 15 is the minimum number
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.
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);
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.
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
srand(time(NULL)); // put this at the beginning of your program
//do stuff
char choice[2] = {'a','b'};
char L = choice[rand()%2];
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............
char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }
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.
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); }
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.