473,889 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05
14 34884

"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
John L wrote:
"Micah Cowan" <mi***@cowan.na me> wrote
sh******@msn.co m (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********@yah oo.com) (cb********@wor ldnet.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)ra nd()/(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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #14
In article <cl************ ****@plethora.n et>, sh******@msn.co m 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.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #15

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

Similar topics

10
11968
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 using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
3
7383
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 have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The code generates a seed based on time(0). I have found that I need to increment
21
3043
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 generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
5
2406
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 can go down similar paths, but not be the same. I will implement code later to make sure i they go more than 10 apart from each other that they are moved closer together, but this is what I have so far, and when the program is run, the two random...
104
5232
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 better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
12
5237
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
21
13525
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
2822
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, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
6
11759
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 that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
24
7252
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 uniformly distributed and unique numbers ?
0
11203
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7999
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.