The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
Jan 16 '07
13 3671
Walter Roberson wrote:
Spiros Bousbouras wrote:
>The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if it always alternated between 2 values ?
It must produce a pseudo-random sequence in the range 0
to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
implementation it can never produce RAND_MAX then RAND_MAX
for that implementation would be defined as the largest value that
it -could- produce, and if that value was not at least 32767 then
the implementation would be non-conforming.
One could similarily argue that if 0 cannot be produced that
the implementation is non-conforming; the argument is perhaps
a bit weaker.
The sequence
u{n+1} = M * u{n} MOD C
with M=16807 and C=2^31-1
is often used as a PRNG.
It does not produce 0. (Otherwise it would always return 0.)
glibc's rand() implementation does not produce 0.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long i = 1;
for (i=1; i; ++i)
{
if (rand() == 0) puts("rand() == 0");
}
return 0;
}
$ /usr/bin/time ./a.out
161.65user 0.08system 2:56.68elapsed 91%CPU
If the implementation alternated between 0 and RAND_MAX then
it would be conforming.
>On the practical side do you have any thoughts on what one could realistically expect from the behaviour of rand() ? Could for example one expect that eventually any value in the range [0,RAND_MAX] will be returned ?
I would not -expect- any self-respecting rand() to not be
able to produce one of the values in the range, eventually;
I would -expect- at worst the sample function given in the
standard. But it wouldn't shock me if some organization
that produced a C-like language used what they -thought-
was a good rand() but which turned out not to be able to produce
some set of values. [NB: the number of organizations that
produce C-like languages appears to far outnumber the ones that
produce conforming C.]
Regards.
Spoon wrote:
glibc's rand() implementation does not produce 0.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long i = 1;
for (i=1; i; ++i)
{
if (rand() == 0) puts("rand() == 0");
}
return 0;
}
$ /usr/bin/time ./a.out
161.65user 0.08system 2:56.68elapsed 91%CPU
Doh! This only means that glibc's rand() does not return 0 after 2^32
invocations. Not that it never returns 0.
Spoon wrote:
Spoon wrote:
>glibc's rand() implementation does not produce 0.
#include <stdio.h> #include <stdlib.h> int main(void) { unsigned long i = 1; for (i=1; i; ++i) { if (rand() == 0) puts("rand() == 0"); } return 0; }
$ /usr/bin/time ./a.out 161.65user 0.08system 2:56.68elapsed 91%CPU
Doh! This only means that glibc's rand() does not return 0 after 2^32
invocations. Not that it never returns 0.
#include <stdlib.h>
int main(void)
{
while (rand() != 0);
return 0;
}
$ /usr/bin/time ./a.out
181.70user 0.03system 3:05.70elapsed 97%CPU
I stand corrected.
What is true is that linear congruential generators with B=0 obviously
never output 0. http://en.wikipedia.org/wiki/Linear_...tial_generator
"Spiros Bousbouras" <sp****@gmail.c omwrote in message
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
Pseudorandom means that it passes or nearly passes a lot of tests for
randomness, but is in fact deterministic.
When you say "would a rand() that always returned the same number/
alternated between 2 values be conforming?" really you are asking a
meaningless question. It just depends on the precise English-language
sentence used in the standard. There are no requirements for RNGs other than
that they be RNGs.
>
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
A decent implementation will do this, but you cannot rely on it. The numbers
will be very roughly uniformly distributed throughout the range, but the
lower bits are allowed to cycle.
Hence
x * rand()/(RAND_MAX + 1.0)
rather than
rand() % x; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wired |
last post by:
Hi,
I've just taught myself C++, so I haven't learnt much about style or
the like from any single source, and I'm quite styleless as a result.
But at the same time, I really want nice code and I go to great
lengths to restructure my code just to look concise and make it more
manageable.
When I say this, I'm also referring to the way I write my functions.
It seems to me sometimes that I shouldn't have many void functions
accepting...
|
by: Gunnar |
last post by:
Hello.
Problem:
How can I select K random values from the elements 0,1,2,3,4...,N-1 ?
I don't know if there's an easy way of doing this, but here are suggestions
for doing it.
One way is to select K random elements (using a rand()% N), and then see if
any number was choosen twice, and then re-select the duplicats until the
|
by: Profetas |
last post by:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks
|
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
|
by: xcm |
last post by:
#include <stdlib.h>
static unsigned long int next = 1;
int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/(2 * (RAND_MAX +1L)) % (RAND_MAX+1L));
}
| |
by: Roka |
last post by:
Hi all.
I'm reading a program which used the sentence below:
#define NUM_THREADS 10
... ...
int rand_num;
rand_num = 1+ (int) (9.0 * rand() / (RAND_MAX + 1.0));
sleep(rand_num);
... ...
(The program is long so I used a part of it.)
|
by: Siam |
last post by:
Hi all,
I'm writing a shell language in c++ that supports the generation of
random numbers, and by its nature, each command must be executed in a
new thread. The call to the random function from my language simply
propogates up to the rand( ) function from c++. For some reason, C++
will give each thread independent use of their own rand( ) function, so
that a rand( ) call from one thread won't affect another thread's call
to rand( )....
|
by: ds |
last post by:
Hi all,
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing...
|
by: Rafael Cunha de Almeida |
last post by:
Hi,
I've found several sites on google telling me that I shouldn't use
rand() % range+1
and I should, instead, use something like:
lowest+int(range*rand()/(RAND_MAX + 1.0))
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |