473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Random Numbers?

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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #1
14 34847
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

Nov 14 '05 #2
On 13 Jan 2004 02:40:40 GMT, sh******@msn.com (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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #3
sh******@msn.com (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) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #4
"Kyle" <sh******@msn.com> wrote in message
news:cl****************@plethora.net...
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #5
In article <cl****************@plethora.net>, Kyle <sh******@msn.com>
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #6
In comp.lang.c.moderated Kyle <sh******@msn.com> 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?

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
--
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #7
sh******@msn.com (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
mi***@cowan.name
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #8

"Micah Cowan" <mi***@cowan.name> wrote in message news:cl****************@plethora.net...
sh******@msn.com (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.
Nov 14 '05 #9
John L wrote:
"Micah Cowan" <mi***@cowan.name> wrote
sh******@msn.com (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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
John L wrote:
"Micah Cowan" <mi***@cowan.name> wrote
sh******@msn.com (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
Nov 14 '05 #11
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.

Nov 14 '05 #12
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #13
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, 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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #14
In article <cl****************@plethora.net>, sh******@msn.com 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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.