Skip navigation
This discussion is archived

random() with Objective C

23518 Views 12 Replies Latest reply: Jul 18, 2008 7:10 AM by Dan Uff RSS
nbixel Calculating status...
Currently Being Moderated
Jul 8, 2008 6:02 PM
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)
  • Tod Kuykendall Level 4 Level 4 (2,270 points)
    Currently Being Moderated
    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...
    G5/2.0x2, Dual XServes x2, XRAID, beige G3 501Mhz
  • Tod Kuykendall Level 4 Level 4 (2,270 points)
    Currently Being Moderated
    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
    G5/2.0x2, Dual XServes x2, XRAID, beige G3 501Mhz
  • hotpaw2 Calculating status...
    Currently Being Moderated
    Jul 8, 2008 9:11 PM (in response to nbixel)
    Documentation:
    From the OS X Terminal, type:
    man 3 random
    MacBook, Mac OS X (10.5.3)
  • dgrassi Level 1 Level 1 (25 points)
    Currently Being Moderated
    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;
    MacBook Pro, Mac OS X (10.5.4)
  • joeisanerd Calculating status...
    Currently Being Moderated
    Jul 9, 2008 10:46 PM (in response to nbixel)
    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)
  • mgjbond007 Calculating status...
    Currently Being Moderated
    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.
    MacBook Pro 17, Mac OS X (10.5.4)
  • mgjbond007 Level 1 Level 1 (0 points)
    Currently Being Moderated
    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.
    MacBook Pro 17, Mac OS X (10.5.4)
  • Dan Uff Level 2 Level 2 (285 points)
    Currently Being Moderated
    Jul 18, 2008 7:10 AM (in response to nbixel)
    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.

    -Dan
    MacBook / Mac Pro / 2 Gigabytes RAM / Member: ADC, Mac OS X (10.5)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.