How to generate random numbers in C

R

Richard Tobin

In a fair game, the player with the biggest house money volume wins.
[/QUOTE]
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
 
B

Bill Reid

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.[/QUOTE]

BWHAHAHAHAHAHAHAHAHAHAHA!!!!

Yeah, I had to come to a "programming" group to have yet another
naive fool "learn me" about the "Martingale" strategy, the oldest and
silliest of all the idiotic gambling systems...
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.

EVENTUALLY God would refuse your bet, because you can't bet
more money than there are atoms in the universe! In the meanwhile,
you'll be down about $100000000000000000000000000000 trillion or
so; now THAT'S a winning "system"!!!

Here's the bottom line so you'll never make this embarrassing
mistake again: there is NO way to bet your money that can turn
a negative expectation game into a positive expecation game.
And no, not even with "unlimited resources", because "unlimited
resources" mean infinity bets and thus, INFINITY LOSSES,
and you can't win money by losing an INFINITE amount of
money, now can you?
 
B

Bill Reid

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 ex-employees
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 pseudo-random numbers seeded from
some split-second event at pseudo-random times...this would
be tough enough under the best of circumstances, and in
a casino environment, purt near impossible, even WITH
"inside information"...
 
R

Richard Tobin

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
 
K

Keith Thompson

Bill Reid said:
BWHAHAHAHAHAHAHAHAHAHAHA!!!!

Yeah, I had to come to a "programming" group to have yet another
naive fool "learn me" about the "Martingale" strategy, the oldest and
silliest of all the idiotic gambling systems...


EVENTUALLY God would refuse your bet, because you can't bet
more money than there are atoms in the universe! In the meanwhile,
you'll be down about $100000000000000000000000000000 trillion or
so; now THAT'S a winning "system"!!!

Here's the bottom line so you'll never make this embarrassing
mistake again: there is NO way to bet your money that can turn
a negative expectation game into a positive expecation game.
And no, not even with "unlimited resources", because "unlimited
resources" mean infinity bets and thus, INFINITY LOSSES,
and you can't win money by losing an INFINITE amount of
money, now can you?

He made no mistake, embarrassing or otherwise. He explained exactly
why the system can't work in practice, and he did so more clearly than
you did (the potential size of the bets is unbounded, but no one bet
or sequence of bets can ever be infinite).
 
C

Chad

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)

What' the difference between random numbers and pseudo-random numbers?
 
B

Bill Reid

Keith Thompson said:
He made no mistake, embarrassing or otherwise. He explained exactly
why the system can't work in practice, and he did so more clearly than
you did (the potential size of the bets is unbounded, but no one bet
or sequence of bets can ever be infinite).

What an idiot. Momma didn't just drop you on your head, she
musta dribbled it like a basketball...

You shudda just declared this to be "off-topic", which is the
safe response for everything you don't understand in life, which
apparently is just about everything...including most noticeably here,
simple logic...

Here, think real hard: if it doesn't work in "practice", and doesn't
work mathematically (where we MUST and ALWAYS deal with the
"limit" of infinity, and HE HIMSELF said it required "unlimited resources",
so add reading comprehension to your list of failures), then it doesn't
work at all...riiiiiiiiiiight?

tee-hee!
<laughing about some dumb dork clutching his head and rolling on
the floor in agony as he tries to wrap his impoverished intellect around
the oldest dumbest gambling myth known to man>
 
B

Bill Reid

Richard Tobin said:
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...
 
F

Flash Gordon

Chad wrote, On 13/04/08 18:22:

What' the difference between random numbers and pseudo-random numbers?

psueudo-random 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.
 
C

Chris Torek

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? :)
 
S

santosh

Flash said:
Chad wrote, On 13/04/08 18:22:



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

Flash Gordon

santosh wrote, On 26/04/08 13:11:
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.
 
S

santosh

Flash said:
santosh wrote, On 26/04/08 13:11:

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

Richard

santosh said:
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.
 
B

Bill Reid

santosh said:
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 high-speed camera(s) to record
the approach of the dice to the wall, a super-fast super-computer
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 split-second 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 pre-determined 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 "non-random"
(read "predictable") due to information about or control of the
"randomizing"
process. In cases where the result was predicted by high-speed
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"...
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top