473,326 Members | 2,076 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,326 software developers and data experts.

random is not random enough?

JNY
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?
Jul 22 '05 #1
25 2930
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


You are forgetting to seed the generator. RTFM on 'srand' function.

V
Jul 22 '05 #2
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


Your missing the srand function. check you docs or google it for details.

Jul 22 '05 #3

"JNY" <jn**@hotmail.com> wrote in message
news:91***********************@posting.google.com. ..
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something?
Yes. Seed the random generator with some number (the number of seconds since
EPOCH, for instance).

See http://www.manpage.org/cgi-bin/man/man2html?query=srand
Is there another random number generator which I could try?


Many.
Jul 22 '05 #4
KPB
Victor Bazarov wrote:
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?

You are forgetting to seed the generator. RTFM on 'srand' function.

V

I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?

What am I missing? I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().

KPB
Jul 22 '05 #5

"KPB" <k@w.net> wrote in message news:VX*******************@fe10.lga...

<snip>
I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?
Ummm...

Right.
What am I missing?
From a quick glance in the standard, it appears that you
aren't missing anything.

I know srand()/rand() is part of (<cstdlib>, <cmath>... I forget which) but don't know about random().


<cstdlib> and <cmath> are C++ header files.

Phew... I'm not the only one who makes mistakes...
Jul 22 '05 #6

JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


Random is not a basic C++ function. The ones you'd want to use are
srand and rand. because of this I'm going to assume a few things.
That is that under the hood this random calls srand once and then calls
rand after that.
If that is the case then your random does exactly what it is supposed
to do.
The rand function is a pseudo random generator. It starts from a seed
number (50 in this case) and based on that value returns you the next
value in a sequence (and uses the returned value to calculate the next
value of the sequence).

Jul 22 '05 #7
KPB
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().

<cstdlib> and <cmath> are C++ header files.


Yes. That I know. In this NG, I considered that info implicit.

If I had said, it's in <random.h>, well, that's not a header file that's
part of the C++ standard library so it wouldn't have been applicable here.
Phew... I'm not the only one who makes mistakes...


I make my share, believe me but I don't think I made one here.
Jul 22 '05 #8
Random is not a basic C++ function.


dangit that should have been
Random is not a basic C++ function IIRC.

(always do cya :) )

Jul 22 '05 #9
KPB wrote:
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().
<cstdlib> and <cmath> are C++ header files.

Yes. That I know. In this NG, I considered that info implicit.


I guess you didn't notice that the thread is cross-posted...
[...]

Jul 22 '05 #10
KPB
Victor Bazarov wrote:
KPB wrote:
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,

<cmath>... I forget which) but don't know about random().


<cstdlib> and <cmath> are C++ header files.


Yes. That I know. In this NG, I considered that info implicit.

I guess you didn't notice that the thread is cross-posted...
[...]


Oops. Nope. That explains a lot.

Sorry to you C guys. I didn't mean to cross-post. This'll be my last one.
Jul 22 '05 #11
KPB wrote:
dandelion wrote:
KPB wrote
I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().


<cstdlib> and <cmath> are C++ header files.


Yes. That I know. In this NG, I considered that info implicit.

If I had said, it's in <random.h>, well, that's not a header file that's
part of the C++ standard library so it wouldn't have been applicable here.


The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
participants does not (officially) know <cstdlib> and <cmath>...
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.
Jul 22 '05 #12
KPB
Michael Mair wrote:
KPB wrote:
dandelion wrote:
KPB wrote

I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().
<cstdlib> and <cmath> are C++ header files.

Yes. That I know. In this NG, I considered that info implicit.

If I had said, it's in <random.h>, well, that's not a header file
that's part of the C++ standard library so it wouldn't have been
applicable here.

The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
participants does not (officially) know <cstdlib> and <cmath>...
Cheers
Michael

Yes because I was an idiot and accidently cross-posted to the C group.
It wasn't my intention and it won't happen again.
Jul 22 '05 #13
KPB wrote:
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().


<cstdlib> and <cmath> are C++ header files.

Yes. That I know. In this NG, I considered that info implicit.


But "this NG" doesn't mean a lot when the thread is cross-posted.

In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
they are topical. The cross-posting is probably inappropriate.

Jul 22 '05 #14
KPB
infobahn wrote:
KPB wrote:
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,

<cmath>... I forget which) but don't know about random().


<cstdlib> and <cmath> are C++ header files.


Yes. That I know. In this NG, I considered that info implicit.

But "this NG" doesn't mean a lot when the thread is cross-posted.

In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
they are topical. The cross-posting is probably inappropriate.


Am I going to have to apologize to each and every one of you or will you
read on to the next posts in this thread first?
Jul 22 '05 #15


KPB wrote:
infobahn wrote:

[snip: KPB missed the fact that the thread is crossposted to both
c.l.c and c.l.c++]
But "this NG" doesn't mean a lot when the thread is cross-posted.

In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
they are topical. The cross-posting is probably inappropriate.


Am I going to have to apologize to each and every one of you or will you
read on to the next posts in this thread first?


Stop apologizing :-)
The problem is that the usenet is asynchronous, so some of us
will not see the answers of others for some minutes, hours or
days after they have been sent.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Jul 22 '05 #16
KPB
Michael Mair wrote:
Stop apologizing :-)
The problem is that the usenet is asynchronous, so some of us
will not see the answers of others for some minutes, hours or
days after they have been sent.


Ok. :-)
Jul 22 '05 #17
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


See Ramdom1 to Ramdom14 in
http://remus.rutgers.edu/~rhoads/Code/code.html

You can use the time (perhaps to the millisecond) as a seed.

gtoomey
Jul 22 '05 #18
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


1) 'random' is not the name of the standard library function for
generating pseudo-random numbers. That function is named 'rand'

2) If rand is used without first seeding with the srand function, it
will provide the same sequence on every run. This is a GoodThing(tm),
since it allows testing and debugging on the same sequence of values.

3) All this and more is covered in the FAQ
<http://www.eskimo.com/~scs/C-faq/top.html>, in particular see the
sections on random numbers, including
13.15 I need a random number generator.
13.16 How can I get random integers in a certain range?
13.17 Each time I run my program, I get the same sequence of numbers
back from rand().
13.18 I need a random true/false value, so I'm just taking rand() % 2,
but it's alternating 0, 1, 0, 1, 0...
13.20 How can I generate random numbers with a normal or Gaussian
distribution?

You will note that 13.17 is just the question that you asked. Always
check the FAQ before posting. Steve Summit has put in a lot of work
making sure that reliable answers are available to common questions.
This ensures that you don't have to put up with incorrect answers from
the clueless and that you don't suffer the anger of people who see the
same, already answered questions appearing here every September, no
matter what month September is in.

Jul 22 '05 #19
On Wed, 29 Dec 2004 10:17:12 -0500, KPB <k@w.net> wrote:
Victor Bazarov wrote:

I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?
random() is a non-standard function (peculiar to DOS implementations, if
not others) that returns a random number between 0 and num-1:

int random(int num)
What am I missing? I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().


#include <stdlib.h>

As this post was cross-posted to comp.lang.c., let me add the obligatory
comment that <cstdlib> and <cmath> are not standard C header files, but
are in fact C++ headers.

The OP's code suggests C++, but since he cross-posted to c.l.c as well,
thought I'd mention it....

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Jul 22 '05 #20
KPB wrote:
dandelion wrote:

I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().
<cstdlib> and <cmath> are C++ header files.

Yes. That I know. In this NG, I considered that info implicit.


Unfortunately, "this NG" is both comp.lang.c and comp.lang.c++, both of
which the OP posted to. That header files are C++ header files is never
implicit in comp.lang.c. In fact, they are off-topic.
I make my share, believe me but I don't think I made one here.


Only the one of failing to note which newsgroups you were posting to.
Jul 22 '05 #21
jn**@hotmail.com (JNY) writes:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


Cross-posting to comp.lang.c and comp.lang.c++ was probably not a good
idea. "cout << x;" is of course specific to C++.

random() is not a standard C function. There is a POSIX function
called random(); your system should have documentation for it.
"man random" if you're on a Unix-like system.

The C standard function for generating random numbers is called
rand(). It should also be documented on your system, and section 13
of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>, has some
good information. (Many implementations of rand() aren't very good;
using a non-standard function like random() might be a good idea.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 22 '05 #22
JNY wrote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


Note that if the program takes less than one second to execute and uses
time() as a seed, rerunning it may produce the same sequence, since
time() has one second resolutions on many implementations.

-Peter

--
Pull out a splinter to reply.
Jul 22 '05 #23

"JNY" <jn**@hotmail.com> wrote in message
news:91***********************@posting.google.com. ..
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?


I do not know the random() function, but if you need another random
function, look at the Mersenne Twister... It is very good...

http://www.math.keio.ac.jp/matumoto/emt.html

There are both C and C++ versions around
Jul 22 '05 #24

"KPB" <k@w.net> wrote in message news:sz******************@fe10.lga...

<snip>
Yes because I was an idiot and accidently cross-posted to the C group.

<snip>

Don't beat yourself up. By that standard, almost everyone on both ng's is an
idiot. With li'l'ol' me right at the top of the list.

Jul 22 '05 #25
On Wed, 29 Dec 2004 22:19:55 +0000, Keith Thompson wrote:

....
random() is not a standard C function. There is a POSIX function
called random(); your system should have documentation for it.
"man random" if you're on a Unix-like system.
My Linux man page says that it is a BSD function, AFAIK it isn't POSIX. It
apparently has a related seeding function called srandom(). The standard C
library (which of course is also available to C++ programs) provides
rand() and srand(). That's a good starting point. Don't try to mix the two
different sets of functions.

To the OP: these are examples of pseudo-random number generators. The
numbers are generated algorithmically, and the algorithms used are
designed to generate sequences that *appear* random and which fare well
for statistical tests for randomness (at least the better algorithms do).
However they aren't random in the true sense, because as you've seen the
sequences are reproducible. If you start in a particular state you will
always end up with the same sequence of numbers. This is why it is
important to "seed" the pseudo-random number generator if you want
different sequences each time.
The C standard function for generating random numbers is called rand().
It should also be documented on your system, and section 13 of the C
FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>, has some good
information. (Many implementations of rand() aren't very good; using a
non-standard function like random() might be a good idea.)


Alternatively include code for the RNG in your program. Good
implementations are written in standard C (or C++ for our fellow readers)
and using such code will not tie your program to a particular system.

Lawrence

Jul 22 '05 #26

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: ma740988 | last post by:
Consider the template member function that generates a random number within a range. #include <cstdlib> #include <ctime> #include <iostream> template<typename T> T Random(T bottom, T top) {
10
by: Johannes Veerkamp | last post by:
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
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
10
by: Barry | last post by:
I am looking for the best way to produce a totally random number in VB.NET with no pre-defined sequence does anybody know if using Randomize followed by rnd is sufficient enough?
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
3
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c */ #include <stdio.h> #include "random.h"...
17
by: almurph | last post by:
Hi, Hope you can help me with this one. I am trying to create random number between 0 and 1 inclusive of cryptographiuc quality. The problems is though - I don't know how! Here is what I have so...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.