473,387 Members | 1,798 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

hi there,
i'm a newbie in c and i'd like to write a programm which generates random
numbers from 1 to 10 (integers). can anybody help me out with the correct
code?

thanx
Nov 14 '05 #1
10 3590
it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??

thanks
Nov 14 '05 #2
Johannes Veerkamp <JV*******@gmx.de> spoke thus:
it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??


Well, besides invoking the wrath of He Who Holds In Thrall on line 42,
you might be misusing rand() - rand() requires a unique seed to give
you anything remotely like random numbers (key word: "remotely").
Check out srand() if that sounds relevant. Otherwise, the Conclave of
C Cages (soft 'c') will be much more able to help you if you actually
describe your code and/or post it.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
Johannes Veerkamp wrote:
it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??

thanks

The rand function returns a pseudorandom integer in the range 0 to
RAND_MAX. Use the srand function to seed the pseudorandom-number
generator before calling rand.

Read your C book. It's all in there.

/J
Nov 14 '05 #4
Check this out.. from glibc manual.

"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

On Fri, 06 Feb 2004 17:59:48 +0100, Johannes Veerkamp wrote:
it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??

thanks


Nov 14 '05 #5
Johannes Veerkamp wrote:
it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??


Show us the program, it helps.

Assuming you use rand(): the random generator in C actually generates a
"pseudo-random" sequence. This is for two reasons:

1) without an external source of randomness (e.g., particular types
hardware, or timed user events) it is /fundamentally impossible/ for a
computer to generate truly random numbers, since the computer must use a
(deterministic) algorithm.

2) it is actually convenient to be able to re-generate the same sequence
all over again. Suppose you do a simulation that uses rand() and at some
point, you see something weird happening. Thanks to the repeatability of
the sequence, you can reproduce the phenomenon.

If you don't like this behavior, look into the srand() function; it is
possible to use an external value as a seed (e.g., the process id, or
the time) to get a non-reproducible sequence. This is hardly ever a good
idea though.

Best regards,

Sidney

Nov 14 '05 #6
Robert wrote:
Check this out.. from glibc manual.

"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."


If you want to generate a pseudo-random sequence portably with
predictable properties, you have little choice other than to implement
one yourself. There's simply to many bad random generators out there;
the glibc-snippet addresses just 1 common problem.

Best regards,

Sidney

Nov 14 '05 #7
Johannes Veerkamp wrote:

it's me again, i answered the question myself, but can anybody tell me
why the programm always generates the same numbers when executing it??


The two questions you have asked are both covered in
the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com
Nov 14 '05 #8
>Assuming you use rand(): the random generator in C actually generates a
"pseudo-random" sequence. This is for two reasons:

1) without an external source of randomness (e.g., particular types
hardware, or timed user events) it is /fundamentally impossible/ for a
computer to generate truly random numbers, since the computer must use a
(deterministic) algorithm.
The source of randomness doesn't necessarily have to be external.
Some processors have hardware for this on-chip (I believe this
includes the Pentium III). But you DO need hardware designed to
generate randomness, as most of the CPU is carefully designed to
avoid randomness (otherwise the chip is generally called "broken").

According to quantum physics, there are certain things that are
fundamentally random and you can get randomness by measuring them.
A couple of these include radioactive decay, thermal noise from a
diode, and there is some argument for randomness from a complex
system such as a human typing when you do keystroke timing below
the microsecond level.
2) it is actually convenient to be able to re-generate the same sequence
all over again. Suppose you do a simulation that uses rand() and at some
point, you see something weird happening. Thanks to the repeatability of
the sequence, you can reproduce the phenomenon.

If you don't like this behavior, look into the srand() function; it is
possible to use an external value as a seed (e.g., the process id, or
the time) to get a non-reproducible sequence. This is hardly ever a good
idea though.


If your point is that it is hardly ever a good idea to try to
generate a non-reproducible sequence, I'll disagree. A sequence
that doesn't change makes games boring. Try running a casino based
on a random-number generator that starts over using the same sequence.
The gamblers will catch on and you'll go broke. On the other hand,
rand() is not nearly good enough in even a majority of C implementations
to run a casino. For one thing, most versions of rand() don't
generate enough random bits or contain enough internal state (32
bits isn't enough).

Gordon L. Burditt
Nov 14 '05 #9
"Gordon Burditt" <go***********@sneaky.lerctr.org> wrote in message
news:c0********@library1.airnews.net...
[...] most of the CPU is carefully designed to
avoid randomness (otherwise the chip is generally called "broken").


<ot>
Overheard in 1995:
Q: Why is pentium so fast?
A: Because it gets the result by guess.
</ot>

Sorry, it's Friday ;-)
Nov 14 '05 #10
>> [...] most of the CPU is carefully designed to
avoid randomness (otherwise the chip is generally called "broken").


<ot>
Overheard in 1995:
Q: Why is pentium so fast?
A: Because it gets the result by guess.
</ot>

Sorry, it's Friday ;-)


As far as I know, Pentium math problems still ended up giving you
the same (sometimes WRONG) answer if you gave it the same numbers
to divide. It's not like it said that 6.0/3.0 is 1.7, no, 2, no
-5, no 2.3, no, 65536, ... .
Gordon L. Burditt
Nov 14 '05 #11

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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.