Duke Leto

Jun 29, 2008, 06:37 PM

Can anyone help me with generation of random numbers in the iPhone SDK?

View Full Version : [iPhone SDK] Random Numbers

Duke Leto

Jun 29, 2008, 06:37 PM

Can anyone help me with generation of random numbers in the iPhone SDK?

firewood

Jun 29, 2008, 07:02 PM

"Real" random numbers, or pseudo-random sequences?

Muncher

Jun 29, 2008, 07:08 PM

If you want a cheap, fast pseudo random number generator, try a linear congruential generator (http://en.wikipedia.org/wiki/Linear_congruential_generator), or a linear feedback shift register (http://en.wikipedia.org/wiki/Linear_feedback_shift_register). They're good, simple generators, but don't trust them for cryptography :rolleyes:.

If you want "real" random numbers, you could try the audio input; feed the LSB into a shift register, and you've got a good source of random numbers.

Then there are also a few websites that feed digits to you online.

If you want "real" random numbers, you could try the audio input; feed the LSB into a shift register, and you've got a good source of random numbers.

Then there are also a few websites that feed digits to you online.

Catfish_Man

Jun 29, 2008, 07:55 PM

Uh, my guess is that all he's looking for is

#include <stdlib.h>

long foo = random();

#include <stdlib.h>

long foo = random();

Duke Leto

Jun 29, 2008, 08:05 PM

Actually I was looking for both encryption and that random function... the random function for a simple RPS game, and random numbers .. for well .. encryption. Encryption is still being a pain though..

rand0m3r

Jun 29, 2008, 10:00 PM

remember that any C code works inside objective-C too.

yeroen

Jun 29, 2008, 10:21 PM

The rule with the random() function is and has always been to only use it for generating seeds to other, better random generators.

Here's a random number generator I included in my own code recently. It's the portable 'ran1' generator recommended by Numerical Recipes in C. It generates uniformly distributed random numbers from the set (0,1) with a period of ~10^9.

#define IA 16807

#define IM 2147483647

#define AM (1.0 / IM)

#define IQ 127773

#define IR 2836

#define NTAB 32

#define NDIV (1 + (IM - 1) / NTAB)

#define EPS 1.2e-7

#define RNMX (1.0 - EPS)

/*

Minimal random number generator of Park and Miller with Bays - Durham shuffle and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate

the largest floating value that is less than 1.

*/

float ran1(long *idum)

{

int j;

long k;

static long iy = 0;

static long iv[NTAB];

float temp;

if (*idum <= 0 || !iy)

{

/*

* Initialize.

*/

if (-(*idum) < 1) *idum = 1;

/* Be sure to prevent idum = 0. */

else *idum = -(*idum);

for (j = NTAB + 7; j >= 0; j--)

{

/*

* Load the shuffle table (after 8 warm - ups).

*/

k = (*idum) / IQ;

*Idum = IA * (*idum - k * IQ) - IR * k;

if (*idum < 0) *idum += IM;

if (j < NTAB) iv[j] = *idum;

}

iy = iv[0];

}

/*

* Start here when not initializing.

*/

k = (*idum) / IQ;

/*

Compute idum = (IA * idum) % IM without overflows by Schrage's method.

*/

* idum = IA * (*idum - k * IQ) - IR * k;

if (*idum < 0) *idum += IM;

/*

* Will be in the range 0..NTAB -1.

*/

j = iy / NDIV;

/*

* Output previously stored value and refill the shuffle table

*/

iy = iv[j];

iv[j] = *idum;

/*

* Because users don't expect endpoint values

*/

if ((temp = AM * iy) > RNMX) return RNMX;

else return temp;

}

If you want to generate uniform random deviates for simulations etc., this is a good algorithm.

For encryption, you're best off using the OpenSSL library versus trying to hand code your own ciphers.

Here's a random number generator I included in my own code recently. It's the portable 'ran1' generator recommended by Numerical Recipes in C. It generates uniformly distributed random numbers from the set (0,1) with a period of ~10^9.

#define IA 16807

#define IM 2147483647

#define AM (1.0 / IM)

#define IQ 127773

#define IR 2836

#define NTAB 32

#define NDIV (1 + (IM - 1) / NTAB)

#define EPS 1.2e-7

#define RNMX (1.0 - EPS)

/*

Minimal random number generator of Park and Miller with Bays - Durham shuffle and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate

the largest floating value that is less than 1.

*/

float ran1(long *idum)

{

int j;

long k;

static long iy = 0;

static long iv[NTAB];

float temp;

if (*idum <= 0 || !iy)

{

/*

* Initialize.

*/

if (-(*idum) < 1) *idum = 1;

/* Be sure to prevent idum = 0. */

else *idum = -(*idum);

for (j = NTAB + 7; j >= 0; j--)

{

/*

* Load the shuffle table (after 8 warm - ups).

*/

k = (*idum) / IQ;

*Idum = IA * (*idum - k * IQ) - IR * k;

if (*idum < 0) *idum += IM;

if (j < NTAB) iv[j] = *idum;

}

iy = iv[0];

}

/*

* Start here when not initializing.

*/

k = (*idum) / IQ;

/*

Compute idum = (IA * idum) % IM without overflows by Schrage's method.

*/

* idum = IA * (*idum - k * IQ) - IR * k;

if (*idum < 0) *idum += IM;

/*

* Will be in the range 0..NTAB -1.

*/

j = iy / NDIV;

/*

* Output previously stored value and refill the shuffle table

*/

iy = iv[j];

iv[j] = *idum;

/*

* Because users don't expect endpoint values

*/

if ((temp = AM * iy) > RNMX) return RNMX;

else return temp;

}

If you want to generate uniform random deviates for simulations etc., this is a good algorithm.

For encryption, you're best off using the OpenSSL library versus trying to hand code your own ciphers.

gnasher729

Jun 30, 2008, 05:09 AM

Can anyone help me with generation of random numbers in the iPhone SDK?

I couldn't tell you anything about random numbers in the iPhone SDK, because that is under NDA, but what makes you think that it would be any different than in any other Unix system? (In Terminal: "man random").

I couldn't tell you anything about random numbers in the iPhone SDK, because that is under NDA, but what makes you think that it would be any different than in any other Unix system? (In Terminal: "man random").

Muncher

Jul 2, 2008, 08:35 PM

The rule with the random() function is and has always been to only use it for generating seeds to other, better random generators.

Here's a random number generator I included in my own code recently. It's the portable 'ran1' generator recommended by Numerical Recipes in C. It generates uniformly distributed random numbers from the set (0,1) with a period of ~10^9.

~SNIP~

If you want to generate uniform random deviates for simulations etc., this is a good algorithm.

For encryption, you're best off using the OpenSSL library versus trying to hand code your own ciphers.

This guy seems to know what he's talking about, listen to him. :D

Here's a random number generator I included in my own code recently. It's the portable 'ran1' generator recommended by Numerical Recipes in C. It generates uniformly distributed random numbers from the set (0,1) with a period of ~10^9.

~SNIP~

If you want to generate uniform random deviates for simulations etc., this is a good algorithm.

For encryption, you're best off using the OpenSSL library versus trying to hand code your own ciphers.

This guy seems to know what he's talking about, listen to him. :D

DenNukem

Jul 12, 2008, 03:31 PM

Two things:

1. Look for SecRandomCopyBytes in SDK documentation. It does not work in emulator (gotta be a bug), but it does work on real device.

2. Using crypto will make you subject to US export regulations. Apple will demand compliance paperwork when you submit app to the app store, regardless of where you plan to sell it. So you might want to reconsider that crypto thing, or seek professional help there.

1. Look for SecRandomCopyBytes in SDK documentation. It does not work in emulator (gotta be a bug), but it does work on real device.

2. Using crypto will make you subject to US export regulations. Apple will demand compliance paperwork when you submit app to the app store, regardless of where you plan to sell it. So you might want to reconsider that crypto thing, or seek professional help there.

gralem

Jul 12, 2008, 04:12 PM

I use these defines for random int's. I call RANDOM_SEED() once in my app, the call something lie RANDOM_INT(1,10) will return a random number between 1 and 10. Works great.

#define RANDOM_SEED() srandom(time(NULL))

#define RANDOM_INT(__MIN__, __MAX__) ((__MIN__) + random() % ((__MAX__+1) - (__MIN__)))

#define RANDOM_SEED() srandom(time(NULL))

#define RANDOM_INT(__MIN__, __MAX__) ((__MIN__) + random() % ((__MAX__+1) - (__MIN__)))