473,614 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3618
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)cybers pace.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.o rg> wrote in message
news:c0******** @library1.airne ws.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

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

Similar topics

10
11942
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
7371
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
3006
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
2390
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
5116
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
5217
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
13499
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
2797
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
11740
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
7199
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
8185
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8583
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...
0
8436
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
6092
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
5542
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
4053
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
4129
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2570
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
0
1429
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.