[comp.lang.c] Random integers

tree@sun.soe.clarkson.edu (Tom Emerson) (03/18/89)

I'm sure this has an easy answer...

I need to generate a random integer between 1 and x.  How can this be done?
I have tried the following:

int k;
k = 10 * drand48();

But to now avail; I get a number that looks similar to 2274665.

Thanks in advance,

Tom Emerson
tree@sun.soe.clarkson.edu

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/18/89)

In article <2686@sun.soe.clarkson.edu> tree@sun.soe.clarkson.edu (Tom Emerson) writes:
>int k;
>k = 10 * drand48();
>But to now avail; I get a number that looks similar to 2274665.

Probably your only problem here is that you allowed the compiler to
assume the default value type of int for the drand48() function; you
need to declare that it returns a double.