P: n/a

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 ?  
Share this Question
P: n/a

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.hiroshimau.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 readonly value for you. It's not something that you set,
it is something that you inquire upon.  
P: n/a

"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.cfaq.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  
P: n/a

In article <9c**********************************@s33g2000pri. googlegroups.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 floatingpoint
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  
P: n/a

what about drand48() function ? Is this included in C standard ?  
P: n/a

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.openstd.org/jtc1/sc22/wg...docs/n1256.pdf
But no, drand48 is not mentioned there. I believe it is POSIX.
Bye, Jojo  
P: n/a

>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 reversebiased 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 pseudorandom numbers. In cryptography, random numbers
are very important and the difference between pseudorandom 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 pseudorandom 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 pseudorandom 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)  
P: n/a

"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 x1.

Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm  
P: n/a

user923005 <dc*****@connx.comwrote in message
news:df**********************************@u69g2000 hse.googlegroups.com...
On Apr 11, 12:20 pm, gordonb.k5...@burditt.org (Gordon Burditt) wrote:
If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudorandom 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 nonreseeded pseudorandom 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 "expectation" 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% "expectation"), 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 nonloss) that conforms to
your "expectation" 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  
P: n/a

"Malcolm McLean" <re*******@btinternet.comwrites:
"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 x1.
rnd(m) can be much closer to m than m1. Did you mean:
#define rnd(x) ((int)((x) * uniform()))
perhaps? The OP wanted numbers between 0 and 1 so your uniform() is
sufficient if they mean doubles in [0,1).

Ben.  
P: n/a

If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudorandom 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 nonreseeded pseudorandom number generator in a casino game and won hundreds of $thousands before the casino realized their error...
My statement is more based on economics and psychology than
mathematics. If there's enough to gain, people will go to great
lengths to break your scheme, including stealing a machine (or
BUYING one) and taking it apart, bribing employees or exemployees
to get source code and procedures, dedicating huge amounts of donated
CPU time under the guise of searching for aliens or a cure for
cancer, etc.
A really clever crack will not take so much in winnings that the
casino realizes it immediately, but will take less but over a longer
period of time.  
P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Malcolm McLean" <re*******@btinternet.comwrites:
>"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 x1.
rnd(m) can be much closer to m than m1. Did you mean:
#define rnd(x) ((int)((x) * uniform()))
perhaps? The OP wanted numbers between 0 and 1 so your uniform() is
sufficient if they mean doubles in [0,1).
That would be fine if int was 64 bits :(

Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm  
P: n/a

In article <IA********************@bgtnsc04news.ops.worldnet.att.net>,
Bill Reid <ho********@happyhealthy.netwrote:
>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 "expectation" of the game.
Actually, you can reliably win against poor odds if you have unlimited
resources and your opponent is required to accept your bets. Decide
how much you want to win. Bet enough to make that if you win. If you
do, quit. If you don't, bet enough to win back your losses plus your
desired win. If you can continue making bets in this manner, you will
eventually win, no matter how poor the odds.
In practice, you can't do this, because your resources are limited
compared to the Casino's. If they weren't, they would eventually
refuse your bet.
 Richard

:wq  
P: n/a

Gordon Burditt <go***********@burditt.orgwrote in message
news:T7ydnSMQJKBSnJzVnZ2dnUVZ_sytnZ2d@internetamer ica...
If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudorandom 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 nonreseeded pseudorandom number generator
in a casino game and won hundreds of $thousands before the
casino realized their error...
My statement is more based on economics and psychology than
mathematics. If there's enough to gain, people will go to great
lengths to break your scheme,
There's a limit to greed; just because somebody offers $1trillion
to run a mile in one second doesn't mean anybody is ever going
to be able to do it, no matter how motivated they are and how
hard they try.
including stealing a machine (or
BUYING one) and taking it apart, bribing employees or exemployees
to get source code and procedures, dedicating huge amounts of donated
CPU time under the guise of searching for aliens or a cure for
cancer, etc.
Sure, this all happens now, and has always happened. Back
when I used to consult for the electronic slot machine and lottery
equipment makers, a popular method of getting money from the
machines was to zap them with about a zillion volts, hopefully
sometimes causing the CPU to fritz out and activate the hopper
dump solenoid and dump the coins in the hopper into the "loud bowl".
Then they'd scoop up as many as they could and try to hit the
doors before security locked them (they were in most cases
already detected and being tracked by security cameras
the moment the coins dropped).
A slightly more sophisticated method involved an operation
using a small drill and a procedure not unlike othroscopic
surgery to trigger a hopper dump; in any event, they still
couldn't fake the internal sequence of microprocessor events
that had to occur and were always recorded that were evidence
of a genuine jackpot, along with those omnipresent security
cameras...
Guys who REALLY knew the machines could even have
a whole set of fake EEPROMs ready to install in seconds
after they jimmied open the door; but seriously, wouldn't
it just be easier to get a friggin' job?
A really clever crack will not take so much in winnings that the
casino realizes it immediately, but will take less but over a longer
period of time.
Oh, they'll figure it out eventually, but the problem will still
be to predict a series of pseudorandom numbers seeded from
some splitsecond event at pseudorandom times...this would
be tough enough under the best of circumstances, and in
a casino environment, purt near impossible, even WITH
"inside information"...

William Ernest Reid  
P: n/a

In article <Hh*********************@bgtnsc04news.ops.worldnet.att.net>,
Bill Reid <ho********@happyhealthy.netwrote:
>And no, not even with "unlimited resources", because "unlimited resources" mean infinity bets and thus, INFINITY LOSSES,
I suggest you learn some mathematics before you rant in future.
All the bets involved are finite.
 Richard

:wq  
P: n/a

On Apr 11, 12:20*pm, gordonb.k5...@burditt.org (Gordon Burditt) wrote:
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 reversebiased 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 pseudorandom numbers. *In cryptography, random numbers
are very important and the difference between pseudorandom 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 pseudorandom 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 pseudorandom 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)
What' the difference between random numbers and pseudorandom numbers?  
P: n/a

Richard Tobin <ri*****@cogsci.ed.ac.ukwrote in message
news:ft***********@pcnews.cogsci.ed.ac.uk...
In article <Hh*********************@bgtnsc04news.ops.worldnet.att.net>,
Bill Reid <ho********@happyhealthy.netwrote:
And no, not even with "unlimited resources", because "unlimited
resources" mean infinity bets and thus, INFINITY LOSSES,
I suggest you learn some mathematics before you rant in future.
"The ironing is delicious"  Bart Simpson
All the bets involved are finite.
Which, according to you, require "unlimited resources"...we
can put Las Vegas out of business, as long as we make a "finite"
series of bets with an infinite amount of money...you're a genius!!!
But it's like I've said before...you guys have GOT to stick to
the return value of main() because everything else on planet
Earth is just too complicated for you...

William Ernest Reid  
P: n/a

Chad wrote, On 13/04/08 18:22:
<snip>
What' the difference between random numbers and pseudorandom numbers?
psueudorandom numbers are numbers that look random (for some definition
of look random) but are really following some sequence (and this, by the
definition of C, includes all implementations of rand()) where as random
numbers really are random.
A quick search on Google will give you a lot more information on pseudo
random numbers.

Flash Gordon  
P: n/a

In article <66**************@mid.individual.net>
Ian Collins <ia******@hotmail.comwrote:
>Why would you attempt to redefine RAND_MAX? ...
Not everyone starts out with the right mental model of RAND_MAX.
It may help to think of RAND_MAX as if it were a highway sign,
telling you how far away the next town or exit is. If you stop
your car, walk up to the sign, paint out the original number, and
then paint in a "1", will the next town or exit move to just 1 mile
(or 1 kilometer) away? :)

InRealLife: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html  
P: n/a

Flash Gordon wrote:
Chad wrote, On 13/04/08 18:22:
<snip>
>What' the difference between random numbers and pseudorandom numbers?
psueudorandom numbers are numbers that look random (for some
definition of look random) but are really following some sequence (and
this, by the definition of C, includes all implementations of rand())
where as random numbers really are random.
I don't think that it can proven that real random sequences exist.
A quick search on Google will give you a lot more information on
pseudo random numbers.
 
P: n/a

santosh wrote, On 26/04/08 13:11:
Flash Gordon wrote:
>Chad wrote, On 13/04/08 18:22:
<snip>
>>What' the difference between random numbers and pseudorandom numbers?
psueudorandom numbers are numbers that look random (for some definition of look random) but are really following some sequence (and this, by the definition of C, includes all implementations of rand()) where as random numbers really are random.
I don't think that it can proven that real random sequences exist.
I think that in Physics they have some pretty good evidence for them,
although the theories might be proven wrong later of course. This is
relevant as some real hardware actually uses some of the principals of
current physics to generate random numbers.

Flash Gordon  
P: n/a

Flash Gordon wrote:
santosh wrote, On 26/04/08 13:11:
>Flash Gordon wrote:
>>Chad wrote, On 13/04/08 18:22:
<snip>
What' the difference between random numbers and pseudorandom numbers? psueudorandom numbers are numbers that look random (for some definition of look random) but are really following some sequence (and this, by the definition of C, includes all implementations of rand()) where as random numbers really are random.
I don't think that it can proven that real random sequences exist.
I think that in Physics they have some pretty good evidence for them,
although the theories might be proven wrong later of course. This is
relevant as some real hardware actually uses some of the principals of
current physics to generate random numbers.
Possible, though I would've thought that presented with a sequence
that's apparently random you cannot conclude that it *is* random since
reading more of the sequence might reveal a pattern or organisation.
And for a sequence of fixed length to say definitively that it's random
would be to close the door to a possibility that future mathematics
could find an organisation not apparent now.
Anyway this is OT and I should take the advice I gave to AT. I'll stop
here. Doubtless this has been beaten to death in sci.math. Interested
lurkers might wish to go there.  
P: n/a

santosh <sa*********@gmail.comwrites:
Flash Gordon wrote:
>santosh wrote, On 26/04/08 13:11:
>>Flash Gordon wrote:
Chad wrote, On 13/04/08 18:22:
<snip>
What' the difference between random numbers and pseudorandom numbers? psueudorandom numbers are numbers that look random (for some definition of look random) but are really following some sequence (and this, by the definition of C, includes all implementations of rand()) where as random numbers really are random.
I don't think that it can proven that real random sequences exist.
I think that in Physics they have some pretty good evidence for them, although the theories might be proven wrong later of course. This is relevant as some real hardware actually uses some of the principals of current physics to generate random numbers.
Possible, though I would've thought that presented with a sequence
that's apparently random you cannot conclude that it *is* random since
Of course you can not. There is not such thing as "defined randomness"
where you can prove a sequence is random. In the real world that is. For
all you know if you measure a sequence then that sequence might well
have been repeated had the sampling gone on. In other words its all hot
air.
reading more of the sequence might reveal a pattern or organisation.
Yes.
And for a sequence of fixed length to say definitively that it's random
would be to close the door to a possibility that future mathematics
could find an organisation not apparent now.
Anyway this is OT and I should take the advice I gave to AT. I'll stop
here. Doubtless this has been beaten to death in sci.math. Interested
lurkers might wish to go there.
Stop being such a big baby  this is a thread. People who dont like it
can kill it or ignore it. Not every one wants to discuss such things
with math egg heads who will probably roll their eyes and humiliate the
C programmer who wants a far lower understanding.  
P: n/a

santosh <sa*********@gmail.comwrote in message
news:fu**********@registered.motzarella.org...
Flash Gordon wrote:
santosh wrote, On 26/04/08 13:11:
Flash Gordon wrote: Chad wrote, On 13/04/08 18:22:
<snip>
What' the difference between random numbers and pseudorandom numbers? psueudorandom numbers are numbers that look random (for some definition of look random) but are really following some sequence (and this, by the definition of C, includes all implementations of rand()) where as random numbers really are random.
I don't think that it can proven that real random sequences exist.
I think that in Physics they have some pretty good evidence for them,
although the theories might be proven wrong later of course. This is
relevant as some real hardware actually uses some of the principals of
current physics to generate random numbers.
Possible, though I would've thought that presented with a sequence
that's apparently random you cannot conclude that it *is* random since
reading more of the sequence might reveal a pattern or organisation.
And for a sequence of fixed length to say definitively that it's random
would be to close the door to a possibility that future mathematics
could find an organisation not apparent now.
I don't think you have to look at quantum effects in physics to
find something that for all intents and purposes is truly "random".
Consider, if you will, a drunk at a craps table...you can't be sure
he won't fling the cubes right off the table, drop them right in front
of himself, or actually make a legal throw to the back of the table,
hitting those little bumps that send them spinning unpredictably
to their final resting place...
And yet, with a sufficiently highspeed camera(s) to record
the approach of the dice to the wall, a superfast supercomputer
dedicated to calculate the trajectories of the dice based on the
inputs from the camera(s) and a model of the countours of the
table, could you "predict" in a splitsecond the result of the dice
throw AFTER the dice were thrown? Be aware that just such
a (sometimes human) system was developed to predict the
quandrant where the ball would land in roulette ("wheel clocking"),
resulting in tremendous profit for the players of the "system"...
And also, some people allegedly can or at least try to "set the
dice": hold them in a predetermined position, and throw them in
such a way that they can somewhat predict the result. This is
so potentially profitable it is totally illegal in all casinos; if they
catch you holding the dice and throwing them in a certain way,
you will be asked to change your behavior, leave, or be arrested...
In those cases, an apparently random system becomes "nonrandom"
(read "predictable") due to information about or control of the
"randomizing"
process. In cases where the result was predicted by highspeed
analysis of the process, the tests that the math wonks apply to
determine randomness would still apply; of course, when the process
is "controlled", the tests will show the process is no longer "random",
at least from that perspective...
So it's always actually a matter of how you look at it, how much you
know about the process, how deftly you can analyze and/or control
the process, that is the final test of whether something is truly "random",
as in "unpredictable". History shows many examples of "unpredictable"
series of events becoming "predictable" as greater knowledge of the
underlying process and/or better analytical capabilities were developed,
and perhaps this will be the case someday for "quantum physics" as
well (Einstein: "God does not play dice with the universe").
But right now, predicting what a drunk in Vegas is going to roll BEFORE
he rolls it can be pretty safely called "impossible"...

William Ernest Reid   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 6374
 replies: 24
 date asked: Apr 11 '08
