random() with Objective C

I need an absolute number generated from 1 to 4. my random number picked 1-4 in C#

picknumber = random.Next(1,5);

documentation on random() for Objective C > DOES NOT TELL YOU HOW TO USE IT

it just lectures you on what it is, but not how its used. Its like me writing a book about apples, about their color, how they grow from trees, how they fall to the ground. But I never ONCE EVER tell you its food and that you eat it. How is this good documentation?

picknumber = random() % 5; is this correct? further, how do I ensure this is an absolute number? sorry just a frustrated at Objective C syntax, and weak sauce documentation.

PC, Mac OS X (10.5.3)

Posted on Jul 8, 2008 6:02 PM

Reply
12 replies

Jul 8, 2008 6:42 PM in response to nbixel

The method random returns a signed long (0 - 0x7FFFFFFF). Using modulo is fine but add 1 if you don't want a possibly zero result.

The reason the documentation is lacking is probably because this old Unix and straight C not Obj-C in any specific way. It is *random (4)* in Unix terms and is actually quite a good routine. If you are really interested in random numbers there are better ways to use it but for everyday uses this is fine.

Hope that helps,

=Tod

PS Tell me you didn't actually just sauce in coding forum...

Jul 8, 2008 7:23 PM in response to Tod Kuykendall

If I try to place anything in random( <<>> ) I get a too many operations error. So random(4) get me that error. I had to ask my coding buddy how this works, but he's an old schooler coder back in the machine language days plus he took all these mathematical computer courses. For those that haven't been to these classes (dunno if they even exist anymore, all the mathematics classes I took involved calculus and I never did learn about the MOD function. Or that is was expressed as %).

I will break it down for you. random() in Objective C generates an Integer of 0-2 power of 32. Or about 4billion. Its an Integer so its going to be an Absolute Value number. Using the MOD function "%" is telling random() to divide that number by n and return an Integer remainder, NOT a remainder remainder (like a fraction).

so for instance 5/4 = 1.25 but using MOD the remainder it is actually 1. Since 4 goes into 5 only once, 5-4 = 1, our remainder AFTER 4 has been subtracted is 1.

random() % 4 is telling random to generate a number between 0-4billion divided by 4, return the remainder.

So no matter what number it generates its going to return a random value between 0 and 3. Here's how

0 / 4 = 0 (since 0/4 = 0 with NO remainder) so result = 0
1 / 4 = 1 (not sure how supply math example here)
2 / 4 = 2 (not sure how supply math example here)
3 / 4 = 3 (not sure how supply math example here)
4 / 4 = 0 (since 4/4 = 1 with NO remainder) so result = 0
5 / 4 = 1 (since 5/4 = 1 with 1 remainder) so result = 1
6 / 4 = 2 (since 6/4 = 1 with 2 remainder) so result = 2
7 / 4 = 3 (since 7/4 = 1 with 3 remainder) so result = 3
8 / 4 = 0 (since 8/4 = 2 with NO remainder) so result = 0
9
10
11
12
etc....

Tod is correct I will have add +1 get a random number of 1-4. so the proper code would look like this

picknumber = 1 + random() % 4;

Jul 8, 2008 8:57 PM in response to nbixel

Sorry, I should have been more explicit. First *random (4)* is the name the Unix routine that you're using to generate the random numbers - it is 4th version of the Unix routine random. I only included that for you if you wanted to look up the official specifications.

Second the modulo command is the easiest way to use the random. There are more complicated ways to use it and you'll find those examples but many of those were created to deal with the random routines before version 4 that weren't as good.

There's a good discussion of the implications of using random here: http://www.cocoadev.com/index.pl?RandomNumber


int x = random % 10; //returns a number between 0 and 9
int y = random % 10 + 1; //returns a number between 1 and 10


There's no reason I can see that y = 1 + random() % 3; shouldn't equal y = random() % 3 + 1; unless there's some precedence issue I can't replicate. You could do y = 1 + (random() % 3); to eliminate the possibility of that.

A couple notes: You can edit your posts for about 15 minutes or so after you post them. You can post code unchanged by bracketing the text by { code } without the spaces.

Hope all that helps,

=Tod

Jul 9, 2008 4:59 AM in response to nbixel

Use arc4random() instead of either random() or rand(). It used /dev/urandom and generates much better pseudo-random numbers. Both rand() and random() are basically bad random number generators.

The arc4random() function returns pseudo-random num-ers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3).

See: man arc4random

#include <stdlib.h>
picknumber = arc4random() % 3 + 1;

Jul 18, 2008 6:19 AM in response to nbixel

I was having the same issue with the random number generator always returning the SAME number, no matter what since I was using time(NULL) as the seed. However, the problem is that I'm calling the random number generator several times within a short period of time and the computer only keeps track of seconds.

How can I generate a bunch of random numbers in a row? If I pass time(NULL), then I will get the same 'random number' several times i a row.

Jul 18, 2008 6:36 AM in response to mgjbond007

Never mind! I was able to answer my own question. The problem is that I'm used to seeing time reported in milliseconds; however, Apple likes to report time in seconds. So, be careful about using time as a seed!

For example, if you're running the random number generator in a loop, change the seed to time(NULL)+i, where i is the index of your loop. That way you are guaranteed a pseudo-random number every time.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

random() with Objective C

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.