This discussion is archived
random() with Objective C
23518 Views
12 Replies
Latest reply:
Jul 18, 2008 7:10 AM by
Dan Uff

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 ObjC 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...G5/2.0x2, Dual XServes x2, XRAID, beige G3 501Mhz
Like (0)


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 02 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, 54 = 1, our remainder AFTER 4 has been subtracted is 1.
random() % 4 is telling random to generate a number between 04billion 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 14. so the proper code would look like this
picknumber = 1 + random() % 4;PC, Mac OS X (10.5.3)
Like (0)


picknumber = 1 + random() % 4;

Like (0)


ooh got a problem still.
picknumber = 1 + random() % 3;
always seems to return as 3. anybody have a solution here?PC, Mac OS X (10.5.3)
Like (0)


dunno why we cant just edit the posts.

Like (0)


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,
=TodG5/2.0x2, Dual XServes x2, XRAID, beige G3 501Mhz
Like (0)


Documentation:
From the OS X Terminal, type:
man 3 randomMacBook, Mac OS X (10.5.3)
Like (0)


Use arc4random() instead of either random() or rand(). It used /dev/urandom and generates much better pseudorandom numbers. Both rand() and random() are basically bad random number generators.
The arc4random() function returns pseudorandom numers 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;MacBook Pro, Mac OS X (10.5.4)
Like (0)


Why does it return 3 all the time? Because you have to seed the "random" number generator. That's because it's not random, it's based on algorithms.
So seed it with a line like
// Generate our random seed
srandom(time(NULL));Macbook Core 2 Dou, Mac OS X (10.5.4)
Like (0)


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.MacBook Pro 17, Mac OS X (10.5.4)
Like (0)


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 pseudorandom number every time.MacBook Pro 17, Mac OS X (10.5.4)
Like (0)


For testing purposes, I would make the last number higher to see if the computer is even randomizing a number, and then slowly decrease the number until either is stops randomizing or I'd get to the last number I want making sure it's still selecting a random number.
DanMacBook / Mac Pro / 2 Gigabytes RAM / Member: ADC, Mac OS X (10.5)
Like (0)
