# Random Numbers (Advanced)

This is a discussion on Random Numbers (Advanced) within the C++ Programming forums, part of the General Programming Boards category; say i wanted to do something at random about 20 percent of the time. the best way i figure is ...

1. ## Random Numbers (Advanced)

say i wanted to do something at random about 20 percent of the time.
the best way i figure is to pick a random number between 0 and 100. pick a range between
0 and 100. the difference in the range is the percent. 20 and 40 is 20 percent;
if the random number is within this range, then "this is the 20% percent of the time".

because what i just typed doesn't make much sense, say i was creating a texas hold'em poker game for example. typically, most players raise before the flop with Ace-King. But to throw people off, some players don't do this and just call. I want to 'just call' about 20 percent of the time and at random. I want to figure out when "20 percent of the time" is.
(btw, i'm not creating a poker game. i've just been thinking about it. that's a little out of my league right now)

1. do you agree with the logic?
2. is there a more elegant way to do this?
3. should this be in the AI section?

Code:
```void main()
{
if( between(  GetRand(0, 100), 1, 20 )  )
dontRaise();

return void; // :-P
}

bool between(int n, int x, int y)
{
int temp;

//min-max
if(x > y)
{
temp = x;
x = y;
y = temp;
}

if(n >= x && n <= y)
return true;
return false;
}

//taken from FAQ
int GetRand(int min, int max)
{
static int Init = 0;
int rc;

if (Init == 0)
{
srand(time(NULL));
Init = 1;
}

rc = (rand() % (max - min + 1) + min);

return (rc);
}```

2. 452 posts and still using void main()?

3. Originally Posted by Shakti
452 posts and still using void main()?
look at my return value

4. look at my errors:
MinGW studio:
main.cpp:6: error: `main' must return `int'
main.cpp:7: error: syntax error before `;' token

MSVC:
error C2062: type 'void' unexpected

5. Originally Posted by Shakti
look at my errors:
MinGW studio: main.cpp:7: error: syntax error before `;' token
MSVC: error C2062: type 'void' unexpected

return void; // :-P (tongue face)

6. Originally Posted by Shakti
452 posts and still using void main()?
Old habits die hard!
#undef _sense_of_humor!

7. Originally Posted by Micko
Old habits die hard!

it's not a habit. i never even used void main. i never could. it was just a little joke tossed in. return void? come on.

i got edited on en poste

8. From Dev-Cpp (MingW port of GCC)
2 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp `main' must return `int'

http://www.research.att.com/~bs/bs_faq2.html#void-main

to do something at random about 20% of the time: each time you get to that point, get a random number in the range 1..100. if the number you picked was in the range 1..20, then do whatever it is you wanted to do.

try something like this:
Code:
```#include<iostream>
#include<cstdlib>
#include<ctime>

int main()
{
int num=0;
srand(static_cast<unsigned int>(time(0)));

for(register int i=0;i<100;i++)
{
if(i%10==0)
{
std::cout<<'\n';
}

if(1+rand()%100>20)
{
std::cout<<"0 ";
continue;
}
else
{
std::cout<<"1 ";
num++;
}
}

std::cout<<"\nOccurrance: "<<num<<'%'<<std::endl;
std::cin.get();
return 0;
}```
and some output:
Code:
```0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
0 0 0 1 0 1 0 1 0 0
0 0 0 0 1 0 0 0 1 0
0 0 1 0 1 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 1 1 0
Occurrance: 20%```

9. That's a really nice example program major.

I think the word you are looking for misplaced, is probability. I believe what you want is to call with a probability of 20% or 1 out of 5.

This is a variation of the popular interview question on reading a random line from a file in one pass (in other words, you can't just read all the lines into memory and pick one from there).

The technique you describe can be simply stated in one line of code, as long as you don't really need a random number between 0 and 100, which I don't think you do.

See whether this works for you:
Code:
```  if ( rand() <= RAND_MAX / 5 ) {
/* ... call ... */
} else {
/* ... raise ... */
}```
What's the probability of rand() returning a number <= RAND_MAX / 5?

HTH,
Will

10. What's the probability of rand() returning a number <= RAND_MAX / 5?
Not very uniform for values of RAND_MAX+1 not congruent to 5.

Besides, I dont think it works.
Let's pick a RAND_MAX for which RAND_MAX+1 is indeed congruent to 5.
So suppose RAND_MAX=9

Now, the range of values in the expression RAND_MAX/5 is [0, 1]
Sure, it is evenly 0 or 1, but that's about it.
The probability is 1/2=0.5, not 0.2 as required.

To make it work, one might try
rand() % 5 == 0, keeping RAND_MAX=9
Now the range of values in the expression (rand() % 5) is [0, 4]
This means that the probability is 2/5=0.2, as required.
But since the value of RAND_MAX+1 is probably 2 with some positive exponent, it wont be congruent to 5, and so the range would not be uniform.

EDIT:
hmm... my analysis is incorrect (forgot we're simply using the whole range to compare with a subset, so the probability is correct), though I suggested a viable alternative, and the non-uniform problem remains.

11. hmm... my analysis is incorrect (forgot we're simply using the whole range to compare with a subset, so the probability is correct), though I suggested a viable alternative, and the non-uniform problem remains.
Thank you for being so forthcoming.

There's two differences that I would like to point out, although I'm pretty sure they are unimportant in this particular case.

First:
Code:
`  rand() % 5`
is a run-time computation.

Code:
`  RAND_MAX / 5`
is a compile-time computation.

Second:
http://www.eskimo.com/~scs/C-faq/q13.16.html compares these two approaches from an implementation perspective. I think you will find it interesting.

As far as uniformity is concerned, I'm not convinced that one method is better or worse. Additionally, I'm not sure that it would even be noticed, and I imagine the fix would outweigh the benefit. In other words, I think you're making a tempest in a teapot, and I would like some further explanation if you would be so kind.

Will

12. compares these two approaches from an implementation perspective. I think you will find it interesting.
That analysis doesnt apply for the same reason why my analysis isnt correct - we're not specifically selecting random numbers, we're using them for probability.

As far as uniformity is concerned, I'm not convinced that one method is better or worse.
They are the same.
To correct that (minor) problem, one can restrict the selection of pseudo-random numbers to the largest range within RAND_MAX congruent to the range required.

In other words, I think you're making a tempest in a teapot, and I would like some further explanation if you would be so kind.
Actually, that's because I originally thought that your method was wrong - but that's a result of insufficient sleep after Lenten Vigil
Sometime I posted it occurred to me that you're not talking about selecting random numbers, but about using them for probability.

Popular pages Recent additions