473,750 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to generate random numbers in C

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 ?
Apr 11 '08 #1
24 7225
On Apr 10, 9:54*pm, pereges <Brol...@gmail. comwrote:
I need to generate two uniform random numbers between 0 and 1 in C ?
How to do it ?
If you have modern hardware and IEEE floating point, I recommend
dsfmt:
http://www.math.sci.hiroshima-u.ac.j...FMT/index.html
otherwise WELL512a.c and WELL512a.h from here:
http://www.iro.umontreal.ca/~panneton/WELLRNG.html

Are another possibility.
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 ?
That is a read-only value for you. It's not something that you set,
it is something that you inquire upon.
Apr 11 '08 #2
"pereges" writes:
>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 ?
There is useful information in the FAQ starting at about 13.15

http://www.c-faq.com/lib/index.html

A set of numbers shouldn't be random AND unique. One of the important
properties of a random number generator is that numbers can be repeated. To
provide what I understand you to want, look into "shuffle" as described in
13.19
Apr 11 '08 #3
In article <9c************ *************** *******@s33g200 0pri.googlegrou ps.com>,
pereges <Br*****@gmail. comwrote:
>I looked into rand function where you need to #define RAND_MAX as 1
RAND_MAX tells you what the maximum random number is. It doesn't
let you control it.
>but will this rand function give me uniformly distributed and unique
numbers ?
>I need to generate two uniform random numbers between 0 and 1 in C ?
rand() returns an integer between 0 and RAND_MAX. It's intended
to be uniformly distributed (though the standard doesn't seem to
say so) so you can get uniformly distributed random floating-point
numbers by something like

((double)rand() ) / RAND_MAX

If you really need *unique* random numbers you're going to have to
do something more complicated. Are you sure you really do?

-- Richard
--
:wq
Apr 11 '08 #4
what about drand48() function ? Is this included in C standard ?
Apr 11 '08 #5
pereges wrote:
what about drand48() function ? Is this included in C standard ?
Why not reading it yourself? At least the drafts are available at no charge,
the latest is to be found at
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf

But no, drand48 is not mentioned there. I believe it is POSIX.

Bye, Jojo
Apr 11 '08 #6
On Apr 10, 9:54 pm, pereges <Brol...@gmail. comwrote:
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 ?
http://www.pobox.com/~qed/random.html

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Apr 11 '08 #7
>I need to generate two uniform random numbers between 0 and 1 in C ?

Computers do not generate truly random numbers without hardware support.
Some techniques include the detection of radioactive decay and thermal
noise from a reverse-biased diode. There is some belief that there
is randomness in the timing (say, down to picoseconds) of keystrokes,
although I don't think anyone has managed to tie human typing to quantum
effects yet. Some CPU chips have hardware for random number generation
on them.

You may want pseudo-random numbers. In cryptography, random numbers
are very important and the difference between pseudo-random numbers
and real random numbers used in encryption may get you killed as a
spy.

If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudo-random numbers, you're
going to lose.

rand() returns the same sequence of numbers each time the program
starts up unless you call srand() with a different seed value from
the last time. Seed values are commonly derived from the time and/or
process ID (but NOT in applications where real random numbers are needed,
like gambling or cryptography: 32 bits of randomness isn't enough, and
a poor seed can cripple a good random number generator).
>How to do it ?
There are better pseudo-random number generators than rand().
These have the disadvantage that they are not included in all C
libraries.
>I looked into rand function where you need to #define RAND_MAX as 1
You do not get to redefine RAND_MAX. Also, the return type of rand()
is int, so don't expect any values where 0 < rand() < 1 . If RAND_MAX
were 1 (not allowed by the standard) all you would get is 0 and 1.
>but will this rand function give me uniformly distributed and unique
numbers ?
Consider using algebra. rand() returns [0, RAND_MAX]. You want
numbers between 0.0 and 1.0 including both endpoints. Or is that just
including 0.0 but not 1.0? Consider what these might give you:

((double)rand() )/RAND_MAX
or
((double)rand() )/(RAND_MAX+1)

Apr 11 '08 #8
"pereges" <Br*****@gmail. comwrote in message news:
>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 ?
No, defining RAND_MAX as something else won't help. All it will do is make
the identifier useless in the rest of your program, without touching the
random number generator. Strictly it is UB in case RAND_MAX appears in some
standard library macro or other.

This is the generally accpeted way to do it
#define uniform() (rand() / (RAND_MAX + 1.0))
#define rnd(x) ( (x) * uniform() )

we add 1.0 to make rand() never return exactly unity, so rnd() is always in
the range 0 to x-1.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 27 '08 #9

user923005 <dc*****@connx. comwrote in message
news:df******** *************** ***********@u69 g2000hse.google groups.com...
On Apr 11, 12:20 pm, gordonb.k5...@b urditt.org (Gordon Burditt) wrote:
If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudo-random numbers, you're
going to lose.
There is no mathematical basis for this general statement, however,
there have been cases where people caught on to the sequence coming
from a poorly- or non-reseeded pseudo-random number generator
in a casino game and won hundreds of $thousands before the
casino realized their error...
The most important factor in gambling is the size of the house.
There is also no mathematical basis for THIS statement...you
guys are batting .000 again...
In a fair game, the player with the biggest house money volume wins.
Oh, a "fair game"...who the hell offers a "fair game"? In any event,
it's irrelevant, because the actual most important factor (to the extent
that we indulge in the pointless semantics of pronouncing a "most
important factor") is the "expectatio n" of the game.
Assuming a Markov process (random walk) the players will get ahead and
behind in a random, wobbling fashion. But as soon as the cash for one
player is gone, the game is over. If you have one hundred dollars and
the opponent has one trillion dollars, you are in a lot of trouble.
If you have a casino, and are stupidly offering a "fair game"
(0% "expectatio n"), you will eventually lose all your $trillion
to salaries and other expenses no matter how many individual
players come in and lose $100, because the money you win
from them will be offset by players who "get lucky" and win
$100, $200, $3000, or more...

You would be correct if you asserted that there is relationship
between the size of your "bankroll" and your average bet size
as a fraction of that "bankroll" in terms of actually acheiving
a result of "bankroll" growth (or non-loss) that conforms to
your "expectatio n" for the game. But that's a slightly more
complicated concept, innit?
That's why I call gambling "A tax on stupidity."
Stupidity is kind of its own tax, innit?

---
William Ernest Reid

Jun 27 '08 #10

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

Similar topics

2
8288
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team B
14
9896
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up to 1 . I am not sure how to do this. Any idea? Thanks.
12
5228
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?
9
9955
by: MyInfoStation | last post by:
Hi all, I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? Thanks a lot, Da
6
2777
by: Anamika | last post by:
I am doing a project where I want to generate random numbers for say n people.... These numbers should be unique for n people. Two people should not have same numbers.... Also the numbers should not be repeted.. Do anyone have some nice algorithm for that? Or anyone can suggest me any type of book or site for that purpose?
20
7851
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them randomly as well. The program should use at least 2 functions. Any help would be greatly appreciated. ...
9
6606
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
19
13675
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
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
9339
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
9256
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...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.