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 ? 13 3670
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 ?
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 ?
It's entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I've seen copy the example
shown in K&R2.]
The FAQ has a number of comments on the issues of rand().
Anyone needing to use random numbers in a serious program will
likely roll their own routine from the plethora of PRNG's available on
the net. [E.g. http://www.stanford.edu/~blp/writings/clc/random.html.]
--
Peter
Peter Nilsson 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 ?
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 ?
It's entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I've seen copy the example
shown in K&R2.]
By "perfect" distribution do you mean uniform distribution ?
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
Spiros Bousbouras <sp****@gmail.c omwrote:
>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.
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.]
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Walter Roberson wrote:
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
Spiros Bousbouras <sp****@gmail.c omwrote:
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.
The standard says "The rand function computes a
sequence of pseudo-random numbers in the range
of 0 to RAND_MAX." I don't see how it follows from
that that the largest value which can be produced
will by definition be RAND_MAX.
In article <11************ **********@51g2 000cwl.googlegr oups.com>,
Spiros Bousbouras <sp****@gmail.c omwrote:
>The standard says "The rand function computes a sequence of pseudo-random numbers in the range of 0 to RAND_MAX." I don't see how it follows from that that the largest value which can be produced will by definition be RAND_MAX.
The standard is rather weak in its description of rand(). It makes
no mention of the distribution of random numbers, so an implementation
that returns 1 99.99999999% of the time could still be conforming.
Presumably this would be considered a quality of implementation issue,
and the same goes for an implementation that never produced RAND_MAX.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Spiros Bousbouras wrote:
Walter Roberson wrote:
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
Spiros Bousbouras <sp****@gmail.c omwrote:
>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.
The standard says "The rand function computes a
sequence of pseudo-random numbers in the range
of 0 to RAND_MAX." I don't see how it follows from
that that the largest value which can be produced
will by definition be RAND_MAX.
According to that definition the sequence:
1,1,1,1...
seems to fit because 1 is between 0 and RAND_MAX.
The rand() function cannot produce negative numbers (by definition)
The rand() function cannot produce numbers larger than RAND_MAX (by
definition).
Generally speaking, a halfway-decent rand() implementation will
eventually produce every number between and including 0 .. RAND_MAX.
However, there is not guarantee in the standard that rand() is halfway
decent (e.g. that it passes Marsaglia's tests).
If you want good pseudo-random numbers, then use the Mersenne Twister.
"Spiros Bousbouras" <sp****@gmail.c omwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
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 ?
Pseudo-random means that the next value of the output is a function of some
internal state. Often, the state and the returned value are the same thing.
Pseudo-random means that the next value is algorithmically predictable, i.e.
it isn't truly random where one could not predict. http://en.wikipedia.org/wiki/Pseudo-random
The most common approach to pseudo-random generators is to use prime moduli. http://en.wikipedia.org/wiki/Linear_...tial_generator
In fact, most people who implement their own random number generators are
pretty happy with the simple one at the link immediately above. I believe
as long as the requirement of coprimality between a couple of the parameters
is met, the generated sequence is guaranteed to hit every value in the
interval. I'm sure all the math is at the link above.
--
David T. Ashley (dt*@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page) http://gpl.e3ft.com (GPL Publications and Projects)
"David T. Ashley" wrote:
>
.... snip ...
>
In fact, most people who implement their own random number
generators are pretty happy with the simple one at the link
immediately above. I believe as long as the requirement of
coprimality between a couple of the parameters is met, the
generated sequence is guaranteed to hit every value in the
interval. I'm sure all the math is at the link above.
Just read Knuth (TAOCP). He has a thorough treatment of linear
congruential generators, including some cookbook tables.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
"CBFalconer " <cb********@yah oo.comwrote in message
news:45******** *******@yahoo.c om...
"David T. Ashley" wrote:
>>
... snip ...
>> In fact, most people who implement their own random number generators are pretty happy with the simple one at the link immediately above. I believe as long as the requirement of coprimality between a couple of the parameters is met, the generated sequence is guaranteed to hit every value in the interval. I'm sure all the math is at the link above.
Just read Knuth (TAOCP). He has a thorough treatment of linear
congruential generators, including some cookbook tables.
Thanks. I have those books on my shelves and actually didn't know that
random number generation was in it (I've used it most for extended-precision
arithmetic and searching and sorting).
I've always admired Knuth. He has a mathematician's gift for brevity.
There is a lot packed into those books.
--
David T. Ashley (dt*@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page) http://gpl.e3ft.com (GPL Publications and Projects) 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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: 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: 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...
| |