 |
 |
Cocoa Random Number Generator?
|
 |
|
 |
|
Forum Regular
Join Date: Apr 1999
Location: London, UK
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
Wow, that's amazingly short.
------------------
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status:
Offline
|
|
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.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|