# Random Numbers?

Discussion in 'C Programming' started by Kyle, Jan 13, 2004.

1. ### KyleGuest

Hello,
I'm trying to get a random number using C. I've tried using random(),
but my program keeps producing numbers that are way out of range from
the numbers I put in.

Anyone got any suggestions?

Kyle
--

Kyle, Jan 13, 2004

2. ### Artie GoldGuest

Kyle wrote:
> Hello,
> I'm trying to get a random number using C. I've tried using random(),
> but my program keeps producing numbers that are way out of range from
> the numbers I put in.
>

See, for example:

http://www.eskimo.com/~scs/C-faq/q13.16.html

(as well as other nearby entries in the FAQ).

HTH,
--ag

--
Artie Gold -- Austin, Texas

Artie Gold, Jan 13, 2004

3. ### illecebraGuest

On 13 Jan 2004 02:40:40 GMT, (Kyle) wrote:

>Hello,
>I'm trying to get a random number using C. I've tried using random(),
>but my program keeps producing numbers that are way out of range from
>the numbers I put in.
>
>Anyone got any suggestions?
>
>Kyle

Use the modulus operator (%) to bring the number in range.

Susan
--

illecebra, Jan 14, 2004
4. ### Keith ThompsonGuest

(Kyle) writes:
> I'm trying to get a random number using C. I've tried using random(),
> but my program keeps producing numbers that are way out of range from
> the numbers I put in.
>
> Anyone got any suggestions?

The multiplication on line 76 of your program should be a division.
And you misspelled "onomatopoeia" in the comment on line 103.

I'm just guessing, of course, since you didn't post anything that
would let us figure out what the actual problem might be.

There is no function in standard C called random(). The standard
defines a rand() function (whose implementation is typically not very
good, unfortunately), so using your system's random() function might
give you better results. I presume you've read the documentation for
the random() function. It probably returns numbers in a wide range;
you'll need to do something to scale them to the range you want.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--

Keith Thompson, Jan 14, 2004
5. ### Peter PichlerGuest

"Kyle" <> wrote in message
news:...
> Hello,
> I'm trying to get a random number using C. I've tried using random(),
> but my program keeps producing numbers that are way out of range from
> the numbers I put in.
>
> Anyone got any suggestions?

FAQ, FAQ, FAQ...!

http://www.eskimo.com/~scs/C-faq/top.html

Look up Q 13.16
--

Peter Pichler, Jan 14, 2004
6. ### Francis GlassborowGuest

In article <>, Kyle <>
writes
>Hello,
>I'm trying to get a random number using C. I've tried using random(),
>but my program keeps producing numbers that are way out of range from
>the numbers I put in.
>
>Anyone got any suggestions?

Yes, try telling us exactly what it is that you want, starting with
whether you need a genuinely random number (int or double) or a
pseudo-random number.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
--

Francis Glassborow, Jan 14, 2004
7. ### Jens SchweikhardtGuest

In comp.lang.c.moderated Kyle <> wrote:
# Hello,
# I'm trying to get a random number using C. I've tried using random(),
# but my program keeps producing numbers that are way out of range from
# the numbers I put in.

So you do something like calling random(34)? That is not how random()
works.

# Anyone got any suggestions?

to turn on all warnings of your compiler. You would have been told your
error if you only had done this.

Once you have done that, the other part of your question is answered in
the clc FAQ.

Regards,

Jens
--
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--

Jens Schweikhardt, Jan 14, 2004
8. ### Micah CowanGuest

(Kyle) writes:

> Hello,
> I'm trying to get a random number using C. I've tried using random(),
> but my program keeps producing numbers that are way out of range from
> the numbers I put in.
>
> Anyone got any suggestions?
>
> Kyle

Well, first off, random() isn't a C function; it's BSD
originally. The C version is rand() (which will return an int,
not long int like random()).

Second, neither random() nor rand() allow you to "put in"
numbers. Look it up in your documentation. It will always return
a value between 0 and an implementation-defined quantity,
RAND_MAX. To use rand() to produce integers in the range from x
to y, inclusive, you should use something like:

int result = x+(int) (y*rand()/(RAND_MAX+1.0));

Make sure that you #include <stdlib.h>; and also that you seed
the generator using srand(), unless you want the same sequence of
numbers every time you run.

--
Micah J. Cowan

--

Micah Cowan, Jan 14, 2004
9. ### John LGuest

"Micah Cowan" <> wrote in message news:...
> (Kyle) writes:
>
>
> Well, first off, random() isn't a C function; it's BSD
> originally.

It may not be specified as part of ISO C but it is stretching
to deny it is a C function. Of course, we know what you mean
but this illustrates a problem with some of the "OT" warnings
we see in clc.

John.

John L, Jan 14, 2004
10. ### CBFalconerGuest

John L wrote:
> "Micah Cowan" <> wrote
> > (Kyle) writes:
> >
> > Well, first off, random() isn't a C function; it's BSD
> > originally.

>
> It may not be specified as part of ISO C but it is stretching
> to deny it is a C function. Of course, we know what you mean
> but this illustrates a problem with some of the "OT" warnings
> we see in clc.

It is not specified in the standard. That means that what it does
and how it should be called is up to the user. To use it in this
group means you should also publish all the source for random(),
else it is impossible to evaluate that use.

int random(int param)
{
return(puts("I don't feel like working today\n", stdout));
}

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Jan 14, 2004
11. ### Tom St DenisGuest

"CBFalconer" <> wrote in message
news:...
> John L wrote:
> > "Micah Cowan" <> wrote
> > > (Kyle) writes:
> > >
> > > Well, first off, random() isn't a C function; it's BSD
> > > originally.

> >
> > It may not be specified as part of ISO C but it is stretching
> > to deny it is a C function. Of course, we know what you mean
> > but this illustrates a problem with some of the "OT" warnings
> > we see in clc.

>
> It is not specified in the standard. That means that what it does
> and how it should be called is up to the user. To use it in this
> group means you should also publish all the source for random(),
> else it is impossible to evaluate that use.
>
> int random(int param)
> {
> return(puts("I don't feel like working today\n", stdout));
> }

puts() doesn't take an FILE* argument. Also "puts" adds a newline so you
don't need one.

Lame script kiddies... when will y'all learn to use a man page or two... If
you're going to be a smartarse about your comment at least be right.

Tom

Tom St Denis, Jan 14, 2004
12. ### GrumbleGuest

Tom St Denis wrote:

> puts() doesn't take an FILE* argument. Also "puts" adds a newline
> so you don't need one.
>
> Lame script kiddies... when will y'all learn to use a man page or
> two... If you're going to be a smartarse about your comment at
> least be right.

Tom,

Don't be a moron, you know he meant fputs(). It's just a typo.

Grumble, Jan 14, 2004
13. ### CBFalconerGuest

Grumble wrote:
>
> Tom St Denis wrote:
>
> > puts() doesn't take an FILE* argument. Also "puts" adds a newline
> > so you don't need one.
> >
> > Lame script kiddies... when will y'all learn to use a man page or
> > two... If you're going to be a smartarse about your comment at
> > least be right.

>
> Tom,
>
> Don't be a moron, you know he meant fputs(). It's just a typo.

I experimentally unplonked him this morning. 3 out of 3 messages
from him have no raison d'etre. Back in the PLONK file.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Jan 14, 2004

Micah Cowan wrote:

> Well, first off, random() isn't a C function; it's BSD
> originally.

I wouldn't say that random() isn't a C function, since it can be
implemented in C. It just isn't part of the Standard C library.

> To use rand() to produce integers in the range from x
> to y, inclusive, you should use something like:
>
> int result = x+(int) (y*rand()/(RAND_MAX+1.0));

That code has three problems:
1. If overflow were not a concern, it would compute a result from x to
x+y-1, inclusive, not x to y.
2. It is likely to overflow, producing undefined results.
3. The distribution is uneven for large values of (y-x).

To get the range x to y, inclusive, the following approximates even
distribution when r=RAND_MAX/(y-x+1) is large.

int result = x+(int) ((y-x+1)*(double)rand()/(RAND_MAX+1.0));

The difference in distribution of different integers in the range x to
y, is, in general, rr+1), assuming that the distribution or rand() is
perfectly flat from 0 to RAND_MAX. To improve this, an iterative scheme
can be used which only scales rand() results which are in the range
0..(floor(RAND_MAX/(y-x+1))*(y-x+1)). Other values are either discarded
and rand() called again, or somehow used with a subsequent rand() result
to approximate a level distribution.

--

15. ### Jerry CoffinGuest

In article <>, says...
> Hello,
> I'm trying to get a random number using C. I've tried using random(),
> but my program keeps producing numbers that are way out of range from
> the numbers I put in.
>
> Anyone got any suggestions?

You've gotten a lot of suggestions. IMO, of what you've received so
far, Thad Smith's post is probably the most thorough and accurate. I'll
add only one detail: he alluded to code that discards values as needed
to produce a flat distribution, but didn't include such code in his
post. Here's some code to do that:

/* return a pseudo-random number between 0 and limit inclusive.
*/
int rand_lim(int limit) {
int divisor = RAND_MAX/(limit+1);
int retval;

do {
retval = rand() / divisor;
} while (retval > limit);

return retval;
}

If you want to specify both a lower and an upper limit, you can call
this specifying the difference between the two, and then add the result
to the lower limit you've specified -- I have code for that, but it's
written as a C++ function that overloads this one, so it isn't topical
here.

Also note that although this is written as a loop, you can usually
expect the loop to execute only once per function call, and chances of
it executing more than twice in a particular call are almost
astronomically remote (unless rand() has massive defects).

--
Later,
Jerry.

The universe is a figment of its own imagination.
--