P: n/a

Hello:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
Can somebody please advise?
Thanks.  
Share this Question
P: n/a

It's a FAQ:
13.16: How can I get random integers in a certain range?
A: The obvious way,
rand() % N /* POOR */
(which tries to return numbers from 0 to N1) is poor, because
the loworder bits of many random number generators are
distressingly *non*random. (See question 13.18.) A better
method is something like
(int)((double)rand() / ((double)RAND_MAX + 1) * N)
If you're worried about using floating point, you could use
rand() / (RAND_MAX / N + 1)
Both methods obviously require knowing RAND_MAX (which ANSI
#defines in <stdlib.h>), and assume that N is much less than
RAND_MAX.
(Note, by the way, that RAND_MAX is a *constant* telling you
what the fixed range of the C library rand() function is. You
cannot set RAND_MAX to some other value, and there is no way of
requesting that rand() return numbers in some other range.)
If you're starting with a random number generator which returns
floatingpoint values between 0 and 1, all you have to do to
get
integers from 0 to N1 is multiply the output of that generator
by N.
References: K&R2 Sec. 7.8.7 p. 168; PCS Sec. 11 p. 172.  
P: n/a

rhitx wrote:
Hello:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
Can somebody please advise?
I see no reason why it would keep returning the same
digit ; how many times did you call it ? But I must point
out that rand_Number % 3 can take the values 0 , 1 , 2
not 1 , 2 , 3 as you want.  
P: n/a

Spiros Bousbouras wrote:
rhitx wrote:
Hello:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
Can somebody please advise?
I see no reason why it would keep returning the same
digit ; how many times did you call it ? But I must point
out that rand_Number % 3 can take the values 0 , 1 , 2
not 1 , 2 , 3 as you want.
After you use the CFAQ to create a range of N consecutive values, just
add the bias.
In this case rand()%3+1 would answer pretty nearly unless he has a
pretty bad PRNG.
I am guessing he is seeding rand with srand(some_constant) over and
over.  
P: n/a

"rhitx" <ju*********@gmail.comwrites:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
It worked when I tried it.
First, a few comments on your code.
The declaration "int random_number()" is legal, but
"int random_number(void)" is clearer and more explicit.
You don't need those temporary variables. The following version of
your function is both shorter and clearer:
int random_number(void)
{
return rand() % 3;
}
Here's a small program I wrote that uses this function:
#include <stdio.h>
#include <stdio.h>
int random_number(void)
{
return rand() % 3;
}
int main(void)
{
int i;
for (i = 0; i < 30; i ++) {
printf(" %d", random_number());
}
putchar('\n');
return 0;
}
And here's its output (I got the same results with your more verbose
version of the function):
0 2 0 2 2 0 0 1 0 2 1 0 2 0 2 0 2 0 0 2 2 1 2 2 2 1 1 0 0 0
So why are you getting 1 every time? It must be something in the code
you didn't show us. My best guess is that you're calling srand() once
for each call to rand(). Don't do that. srand() is used to
initialize a sequence of random numbers; normally, you should only
call it once in the entire execution of your program. Consult your
documentation for the rand() and srand() functions.
See also the comp.lang.c FAQ, <http://www.cfaq.com/>, questions 13.15
through 13.21.

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.  
P: n/a

On 6 Dec 2006 17:41:28 0800, "rhitx" <ju*********@gmail.comwrote:
>Hello:
I'm trying create a random number generator, which will generate either number 1, 2, 3.
I tried to do it like:
int random_number() { int no_goal, rand_Number; rand_Number = rand(); no_goal = (rand_Number % 3);
If you want the range specified, you need to add 1 here.
> return no_goal; }
But it is only generating the digit "1", I don't see it changing to 2 or 3.
Either you are calling the function only once, or you forgot the
srand() function.  
P: n/a
 dc*****@connx.com wrote:
>
Spiros Bousbouras wrote:
rhitx wrote:
Hello:
>
I'm trying create a random number generator,
which will generate
either number 1, 2, 3.
>
I tried to do it like:
>
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
>
But it is only generating the digit "1",
I don't see it changing to 2 or 3.
Can somebody please advise?
I see no reason why it would keep returning the same
digit ; how many times did you call it ? But I must point
out that rand_Number % 3 can take the values 0 , 1 , 2
not 1 , 2 , 3 as you want.
After you use the CFAQ to create a range
of N consecutive values, just add the bias.
In this case rand()%3+1 would answer pretty nearly unless he has a
pretty bad PRNG.
I am guessing he is seeding rand with srand(some_constant) over and
over.
I'm guessing that he's doing that by successive executions
of a program which only calls the function once.
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define ONE_HUNDRED 20
#define random_number() (rand() % 3 + 1)
int (random_number)(void);
int main(void)
{
int x, y;
for (x = 0; x != ONE_HUNDRED; ++x) {
for (y = 0; y != ONE_HUNDRED; ++y) {
printf("%d ", random_number());
}
putchar('\n');
}
return 0;
}
int (random_number)(void)
{
return random_number();
}
/* END new.c */

pete  
P: n/a

In article <11*********************@16g2000cwy.googlegroups.c om"rhitx" <ju*********@gmail.comwrites:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
The low order bits are highly nonrandom. See the FAQ, question 3.16:
<http://cfaq.com/lib/randrange.html>.

dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/  
P: n/a

Dik T. Winter wrote:
>
In article <11*********************@16g2000cwy.googlegroups.c om>
"rhitx" <ju*********@gmail.comwrites:
I'm trying create a random number generator,
which will generate either number 1, 2, 3.
no_goal = (rand_Number % 3);
The low order bits are highly nonrandom.
Even if the lower order bits were constant,
I don't see how that would be deleterious to a {1,2,3} prng
which uses a remainder operator with a right operand of 3.
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define TWENTY 20
int main(void)
{
int x, y;
int three_lower_order_bits_are_constant;
for (x = 0; x != TWENTY; ++x) {
for (y = 0; y != TWENTY; ++y) {
three_lower_order_bits_are_constant
= rand() * 8u % 3 + 1;
printf("%d ", three_lower_order_bits_are_constant);
}
putchar('\n');
}
return 0;
}
/* END new.c */

pete  
P: n/a

Raymond,
You're right, I was calling the function once. I didn't realize
that.
Thanks a lot guys!
Raymond Martineau wrote:
On 6 Dec 2006 17:41:28 0800, "rhitx" <ju*********@gmail.comwrote:
Hello:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
If you want the range specified, you need to add 1 here.
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
Either you are calling the function only once, or you forgot the
srand() function.
 
P: n/a

pete wrote:
>
Dik T. Winter wrote:
In article <11*********************@16g2000cwy.googlegroups.c om>
"rhitx" <ju*********@gmail.comwrites:
I'm trying create a random number generator,
which will generate either number 1, 2, 3.
no_goal = (rand_Number % 3);
The low order bits are highly nonrandom.
Even if the lower order bits were constant,
I don't see how that would be deleterious to a {1,2,3} prng
which uses a remainder operator with a right operand of 3.
/* BEGIN new.c */
I think this might express the idea better,
since in this one, the three order bits are actually constant
in the object called "three_lower_order_bits_are_constant".
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define SHOW 0
#define TWENTY 100
int main(void)
{
int x, y;
int three_lower_order_bits_are_constant;
int numcount[3] = {0};
for (x = 0; x != TWENTY; ++x) {
for (y = 0; y != TWENTY; ++y) {
three_lower_order_bits_are_constant = rand() * 8u;
++numcount[three_lower_order_bits_are_constant % 3];
#if SHOW != 0
printf("%d ", three_lower_order_bits_are_constant % 3 + 1);
#endif
}
#if SHOW != 0
putchar('\n');
#endif
}
printf("%d ones\n", numcount[0]);
printf("%d twos\n", numcount[1]);
printf("%d threes\n", numcount[2]);
return 0;
}
/* END new.c */

pete  
P: n/a

rhitx wrote:
>
You're right, I was calling the function once. I didn't realize
that.
Please don't toppost. Your answer belongs after, or intermixed
with, the _snipped_ material you quote. See the following links.

Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smartquestions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>  
P: n/a

"rhitx" <ju*********@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
Hello:
I'm trying create a random number generator, which will generate
either number 1, 2, 3.
I tried to do it like:
int random_number()
{
int no_goal, rand_Number;
rand_Number = rand();
no_goal = (rand_Number % 3);
return no_goal;
}
But it is only generating the digit "1", I don't see it changing to 2
or 3.
The problem may be, if you are calling it only once per program invocation,
that the library starts off with the same seed every time. Try calling it
maybe 10 times in one invocation of the program.  
P: n/a

pete wrote:
pete wrote:
>>Dik T. Winter wrote:
>>>In article <11*********************@16g2000cwy.googlegroups.c om> "rhitx" <ju*********@gmail.comwrites: I'm trying create a random number generator, which will generate either number 1, 2, 3.
> no_goal = (rand_Number % 3);
>>>The low order bits are highly nonrandom.
They may be true for some PRGs, certainly not for all, making the
statement misleading.
>>Even if the lower order bits were constant, I don't see how that would be deleterious to a {1,2,3} prng which uses a remainder operator with a right operand of 3.
Good point! Since n is relatively prime to (1 << intbits), a few
nonrandom low bits shouldn't have an effect.
I think this might express the idea better,
since in this one, the three order bits are actually constant
in the object called "three_lower_order_bits_are_constant".
/* BEGIN new.c */
Nice demo.

Thad   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 4381
 replies: 13
 date asked: Dec 7 '06
