Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Please help with randomization!

Please help with randomization!
Thread Tools
Varun Mehta
Fresh-Faced Recruit
Join Date: Oct 2001
Location: New York, USA
Status: Offline
Reply With Quote
May 18, 2002, 10:50 AM
 
I've been programming in C and REALBasic for a long time now, and as a developer have finally decided to move to Objective-C and OS X. I'm trying to generate random numbers, but the strangest anomaly keeps occurring. when I rand()%6 the results always alternate between odd and even!

So my output usually goes like this

0 3 2 5 4 1 2 3

What do you use for your srand() if you think it's needed? Could bad seed initialization be the problem? Is there a better way in Cocoa apps to generate random numbers?
Insert Witty Comment Here
     
Seb G
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
May 18, 2002, 12:37 PM
 
Funny. rand() seems to generate those alternatingly odd and even sequences on Mac OS - works better on Sun. Doesn't seem to depend on the seed either. Try

(rand()/2) % 6 /* works for me, get: 0 0 3 1 2 2 1 3 4 5 5 ... */

or better use random() and srandom(time(NULL)) instead. This calls a "better" random number generator (see also 'man random' in the terminal):

random()%6 + 1 /* gives 1 0 1 2 3 3 0 5 1 0 1 3 5 ... */
     
Apocalypse
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
May 18, 2002, 12:52 PM
 
This is common to many implementations of random functions. For this reason, you are often instructed to perform integer division by some large number to ignore the lower-order bits of the generated number.

Sorry to be telling you that mod is exactly what not to do.

Hope that helps,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
skipjack
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
May 18, 2002, 01:27 PM
 
It sounds like you know what's going on, so I'm not sure what the problem is. The rand function generates a pseudo-random integer and you should get the same sequence. (In fact, P. J. Plauger states that it is desirable to get the same sequence every time for debugging purposes in his "The Standard C Library.") The srand function varies the starting point in that sequence, so you need to call both rand and srand. (If rand is called before srand, the sequence generated is for srand with a seed of 1.) Srand is commonly seeded with the time() function. This is standard C.

I don't see any additional methods in C++. As mentioned previously, the low-order bits are often suspect using rand() %n. Stroustrup says this: "Often int((double(rand())/RAND_MAX)*n) gives acceptable results. However, to seriously use that formula, we must take care of the minuscule probability that the result will be n."

Stroustrup provides a class representation of a random number generator in his "The C++ Programming Language".

[ 05-18-2002: Message edited by: skipjack ]
     
markko
Fresh-Faced Recruit
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 18, 2002, 07:05 PM
 
Originally posted by Varun Mehta:
<STRONG>I've been programming in C and REALBasic for a long time now, and as a developer have finally decided to move to Objective-C and OS X. I'm trying to generate random numbers, but the strangest anomaly keeps occurring. when I rand()%6 the results always alternate between odd and even!

So my output usually goes like this

0 3 2 5 4 1 2 3

What do you use for your srand() if you think it's needed? Could bad seed initialization be the problem? Is there a better way in Cocoa apps to generate random numbers?</STRONG>
As far as i know this is just what you can expect from the standard ANSI C implementation. Randomization with srand() , as fa as i know, as before, is always needed. For simple stuffs i just include time.h and use

srand(time(NULL))

as randomization seed.
just my two cents
------------------------------------------
Gnu the world!!!
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 21, 2002, 04:45 PM
 
Don't use srand/rand. Use srandom()/random() instead. It's not as portable, but it's a much much better random number generator. rand() is awful. If you need portability, find the source code to a generator out there. Mersenne Twister is good, for one, and there are plenty of others. Just about anything is better than rand().
     
skipjack
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
May 21, 2002, 07:34 PM
 
Originally posted by lindberg:
<STRONG>Don't use srand/rand. Use srandom()/random() instead. It's not as portable, but it's a much much better random number generator. rand() is awful. If you need portability, find the source code to a generator out there. Mersenne Twister is good, for one, and there are plenty of others. Just about anything is better than rand().</STRONG>
Sorry for being such a rookie, but I can't find any reference to srandom()/random() in "The C++ Programming Language" (1997), "The Draft Standard C++ Library" (1995), or "The Standard C Library" (1992). I'm not a programmer and I know my references may be a bit out of date. How do we get these functions?
     
Seb G
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
May 22, 2002, 05:17 AM
 
They are in the Darwin stdlib, i.e. you
#include &lt;stdlib.h&gt;
as you would for rand() and srand(). They are declared as
long random(void);
and
void(srandom(unsigned seed);
You get the documentation by typing 'man random' in the terminal.

The random()/ srandom() have (almost) the same calling sequence and initialization properties as rand(3)/ srand(3). The difference is that rand produces a much less random sequence -- in fact, the low dozen bits generated by rand go through a cyclic pattern. All the bits generated by random() are usable. For example, `random()&01' will produce a random binary value.
(from the manpage)
     
skipjack
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
May 22, 2002, 12:50 PM
 
Originally posted by Seb G:
<STRONG>They are in the Darwin stdlib</STRONG>
Thanks! I guess this means I need to update my C language references.
     
Amorph
Dedicated MacNNer
Join Date: Mar 2001
Location: Iowa City, IA
Status: Offline
Reply With Quote
May 22, 2002, 03:08 PM
 
You won't have any luck with updating your references, because srandom() and random() aren't part of the C or C++ languages.

They are, however, common on UNIXen, and possibly even POSIX standard (I'm not sure about that, though).
James

"I grew up. Then I got better." - Sea Wasp
     
Mskr
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
May 24, 2002, 12:02 PM
 
Originally posted by Amorph:
<STRONG>You won't have any luck with updating your references, because srandom() and random() aren't part of the C or C++ languages.

They are, however, common on UNIXen, and possibly even POSIX standard (I'm not sure about that, though).</STRONG>
According to UNIX Systems Programming for SVR4 (great book, btw), pg 485:

<STRONG>Some versions of UNIX, usually those based on BSD, also suppy <font face = "courier">random</font> and <font face = "courier">srandom</font>, with similar semantics.</STRONG>

So, it appears to not be POSIX, but rather a BSD UNIX extension, thus included in Mac OS X's libc. The Linux manpage that I have access to right now says that glibc's version (2.2.5 on my Debian box) complies with BSD 4.3. It also gives a more technical description of the entropy function that is used. The rand() manpage also gives some very good pointers on more technical discussion of random number generation:

<STRONG><font face = "courier">In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"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)."

Random-number generation is a complex topic. The Numeri�
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).

For a more theoretical discussion which also covers many
practical issues in depth, please see Chapter 3 (Random
Numbers) in Donald E. Knuth's The Art of Computer Program�
ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read�
ing, Massachusetts: Addison-Wesley Publishing Company,
1981.
</font></STRONG>

I know, I know, too much information....
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:45 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,