Quick rand() question:
I know you're not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me that
given that the argument is not a power of two (or near a power of
two), that this is not an issue. The upper bits will participate
equally in the result with the lower. Am I missing something?
Thanks!
-- Rich -- 15 3945
On Oct 22, 6:04 pm, Rich Fife <rf...@amug.org wrote:
Quick rand() question:
I know you're not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me
that given that the argument is not a power of two (or near a
power of two), that this is not an issue. The upper bits will
participate equally in the result with the lower. Am I missing
something?
Yes, you are missing some mathematical analysis to back up what you
just said. If you do (rand() % 1023) on Microsoft Visual C++ or
WATCOM C/C++, 32 of the possible outputs will have an extra 3% bias no
matter how good your random number generator is. No C compiler's
rand() that I have ever seen has, by itself, a worse effect on random
output than that.
The C.L.C. FAQ about this gives extremely misleading advice on this
point and it should seriously be ignored. If you want to seriously
deal with random numbers just read my page about it: http://www.pobox.com/~qed/random.html
I build up a *REAL* ranged random number generator with reasonable
performance characteristics , culminating in the randrange() function
that removes all the primary problems with ranged random numbers. If
you want something with pure random bit quality you can always use the
Mersenne Twister or Fortuna as a base for my generator function.
--
Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/
Paul Hsieh <we******@gmail .comwrote:
No C compiler's
rand() that I have ever seen has, by itself, a worse effect on random
output than that.
Then you've never seen the truely bad BSD rand() that fostered most of
the paranoia about rand. With it, rand() % 1 generated the sequence 0,
1, 0, 1, 0, 1, ....
--
Larry Jones
This game lends itself to certain abuses. -- Calvin
On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote:
Paul Hsieh <we******@gmail .comwrote:
>No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that.
Then you've never seen the truely bad BSD rand() that fostered most of
the paranoia about rand. With it, rand() % 1 generated the sequence 0,
1, 0, 1, 0, 1, ....
That would be bad, as rand() % 1 should only ever produce 0.
Kelsey Bjarnason <ke*****@lgisp. netwrites:
On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote:
>Paul Hsieh <we******@gmail .comwrote:
>>No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that.
Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, ....
That would be bad, as rand() % 1 should only ever produce 0.
Obviously Larry is using a font that doesn't distinguish clearly
enough between '%' and '&'. Yeah, that's it.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" la************@ siemens.com wrote:
Paul Hsieh <we******@gmail .comwrote:
>No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that.
Then you've never seen the truely bad BSD rand() that fostered
most of the paranoia about rand. With it, rand() % 1 generated
the sequence 0, 1, 0, 1, 0, 1, ....
FYI the value of "rand() % 1" is identically 0. Except it may be
considerably slower than just writing "0".
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Keith Thompson <ks***@mib.orgw rites:
Kelsey Bjarnason <ke*****@lgisp. netwrites:
>On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote:
>>Paul Hsieh <we******@gmail .comwrote: No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that.
Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, ....
That would be bad, as rand() % 1 should only ever produce 0.
Obviously Larry is using a font that doesn't distinguish clearly
enough between '%' and '&'. Yeah, that's it.
I had presumed that ``const int l=2;'' was the line before.
Obfuscatorially yours,
Phil
--
The fact that a believer is happier than a sceptic is no more to the
point than the fact that a drunken man is happier than a sober one.
The happiness of credulity is a cheap and dangerous quality.
-- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion
CBFalconer wrote:
la************@ siemens.com wrote:
>Paul Hsieh <we******@gmail .comwrote:
>>No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that.
Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, ....
FYI the value of "rand() % 1" is identically 0. Except it may be
considerably slower than just writing "0".
Not if your compiler has a good optimizer... ;-)
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Charles Richmond <fr*****@tx.rr. comwrites:
CBFalconer wrote:
>la************@ siemens.com wrote:
>>Paul Hsieh <we******@gmail .comwrote:
No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that. Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, ....
FYI the value of "rand() % 1" is identically 0. Except it may be considerably slower than just writing "0".
Not if your compiler has a good optimizer... ;-)
Maybe. rand() has side effects, so a call to it can't be optimized
away, *unless* the compiler can prove that the side effects don't
affect the program's output.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson wrote:
Charles Richmond <fr*****@tx.rr. comwrites:
>CBFalconer wrote:
>>la************@ siemens.com wrote: Paul Hsieh <we******@gmail .comwrote:
No C compiler's rand() that I have ever seen has, by itself, a worse effect on random output than that. Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, .... FYI the value of "rand() % 1" is identically 0. Except it may be considerabl y slower than just writing "0".
Not if your compiler has a good optimizer... ;-)
Maybe. rand() has side effects, so a call to it can't be optimized
away, *unless* the compiler can prove that the side effects don't
affect the program's output.
srand( rand() % 1 );
--
Eric Sosman es*****@ieee-dot-org.invalid This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom McCallum |
last post by:
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.
Thanks for your help in advance,
Tom
|
by: Chris Gordon-Smith |
last post by:
I have a simulation program that calls the rand() function at various points
while it executes.
For the user interface that displays statistics etc. while the program runs,
I use the Lazarus GUI library, which is based on GTK. Depending on which
libraries I link in, I can have either a GTK1 or GTK2 user interface.
I'm finding that the simulation behaves differently depending on whether I
link with GTK1 or GTK2.
|
by: Ben Justice |
last post by:
For a program in c, I need some random numbers for a system were people are
placing bets. This is not a commerical project btw. Generally, I tend to
rely on things from the standard library, because they're written by people
with skills far above mine. Hence, I've always used rand() and FALSELY
assumed it could produce unpredictable random numbers and could be used in
many situations even if security was an issue.
Firstly, lets assume...
|
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: Jack |
last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that
the same value will not appear twice?
And is this a floating window?
For example, if RAND_MAX is 32767, and I make 500,000 consecutive rand
calls then is the rand() algorithm going to guarantee me that no
floating window of calls over that 500,000 rand calls will have the
same value twice? The only way that would work is if the series was
identical everytime.
| |
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: Spiros Bousbouras |
last post by:
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
will be returned ?
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: 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();...
| |
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...
| |