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 > Cocoa Random Number Generator?

Cocoa Random Number Generator?
Thread Tools
Forum Regular
Join Date: Apr 1999
Location: London, UK
Status: Offline
Reply With Quote
Apr 18, 2001, 12:00 PM
 
Is there a function or class in the Cocoa frameworks or the CoreFoundation that allows me to easily get a random integer?
Didn't find anything in the documentation.
     
Junior Member
Join Date: Apr 2001
Location: Seattle, WA, USA
Status: Offline
Reply With Quote
Apr 18, 2001, 12:27 PM
 
Originally posted by asagoo:
Is there a function or class in the Cocoa frameworks or the CoreFoundation that allows me to easily get a random integer?
Didn't find anything in the documentation.
There's nothing in Cocoa or Core for this because it is already in BSD.

Code:
% man random RANDOM(3) System Programmer's Manual RANDOM(3) NAME random, srandom, initstate, setstate - better random number generator; routines for changing generators SYNOPSIS #include <stdlib.h> long random(void) void srandom(unsigned seed) char * initstate(unsigned seed, char *state, int n) char * setstate(char *state) DESCRIPTION The random() function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to (2**31)-1. The maximum value returned by random() is LONG_MAX (as defined by the header file <limits.h>). The period of this random number generator is very large, approximately 16*((2**31)-1).
     
Dedicated MacNNer
Join Date: Mar 2001
Location: Iowa City, IA
Status: Offline
Reply With Quote
Apr 18, 2001, 02:16 PM
 
Not to mention C's own rand(), also in <stdlib.h> (and more portable than random()).


------------------
James

"I grew up. Then I got better." - Sea Wasp
James

"I grew up. Then I got better." - Sea Wasp
     
Professional Poster
Join Date: Jan 2000
Status: Offline
Reply With Quote
Apr 18, 2001, 11:42 PM
 
Man. I knew a guy in grad school that used to freak over random number generators. He was setting out to write his own because he didn't trust the C ones. Like he could do a better job? PLUS his work wasn't sensitive enough to pick up on a bad random number generator anyway. Freak!
     
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status: Offline
Reply With Quote
Apr 18, 2001, 11:56 PM
 
Supposedly the Merseen Twister (sp?) is the current best random number generator. But, I think only PHP uses it natively (they have the old one, too).

------------------
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 19, 2001, 07:16 AM
 
Of course, if you don't care about getting millions of numbers per second, your best bet is to just find as many statistics as you can and manipulate them together (core processor temperature, audio in, &c.).

You can get the Mersenne Twister code here:
http://www.math.keio.ac.jp/~matumoto/emt.html
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status: Offline
Reply With Quote
Apr 19, 2001, 10:52 AM
 
Originally posted by parallax:
You can get the Mersenne Twister code here:
http://www.math.keio.ac.jp/~matumoto/emt.html
Wow, that's amazingly short.

------------------
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Apr 21, 2001, 08:49 PM
 
Originally posted by mr_sonicblue:
Supposedly the Merseen Twister (sp?) is the current best random number generator. But, I think only PHP uses it natively (they have the old one, too).
MT is very cool, and it is really fast, but it does not have any intrinsic methods for entropy gathering, and it is not cryptographicly secure. For serious crypto grade numbers I recommend Yarrow. Apple has a userlevel implementation of it as part of SecurityServer (though I think the interfaces are private), and I have ported a kernel level version from FreeBSD. My patches are available from http://www.rpi.edu/~gerbal/darwin-patches/.

Louis

------------------
Louis Gerbarg
Darwin Developer
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2