Random Numbers?

K

Kyle

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
 
I

illecebra

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
 
K

Keith Thompson

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.
 
F

Francis Glassborow

Kyle <[email protected]> said:
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.
 
J

Jens Schweikhardt

# 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?

Read about how random works, what header declares its prototype and how
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
 
M

Micah Cowan

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.
 
J

John L

Micah Cowan said:
(e-mail address removed) (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.
 
C

CBFalconer

John said:
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));
}
 
T

Tom St Denis

CBFalconer said:
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
 
G

Grumble

Tom said:
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.
 
C

CBFalconer

Grumble said:
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.
 
T

Thad Smith

Micah said:
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, r:(r+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.

Thad
 
J

Jerry Coffin

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top