arc4random () % slider.value ?

Discussion in 'iOS Programming' started by peacetrain67, Sep 16, 2009.

  1. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #1
    title pretty much explains it, i want to use the value of a slider as the range - I know I have to convert the value to an integer and such, but my attempts keep yielding errors. Quickest way to do it?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    First, forget about arc4random. It's a waste of your time. Use random().

    Show your code and tell what errors you're getting. You may need to typecast the slider.value because it returns a float. Cast it to int.
     
  3. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #3
    i figured it out actually, just hadnt got around to posting the solution until now, and just happened to have your comment- anyways, heres the simplest way to convert a slider's value:
    Code:
    		int xRandoms = (int)(randomBallBasicXChange.value);
    		randomicityXresult = 1 + arc4random() % xRandoms;
    randomBallBasicXChange is my slider - randomicityXresult returns the random integer that I use elsewhere - xRandoms is the integer the float is rounded off to and translated.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Right. You can't use the modulus operator with floats.

    You should still dump arc4random(). It's slower than random() and for most uses will give you no better results. I assume you're not doing encryption with the value of your slider.
     
  5. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #5
    on the guy who made the most popular iphone book, he has a blog and he compared arc4random() and rand()
    he had comparisions and in the comments someone said rand() was quicker and there is math there and it shows that realistically you aren't getting any performance boosts from using it as well as the fact you don't have to seed arc4random and the results given are almost identical, if you haven't seen the blog post i can look around on the internet, but as i said its on the blog for one of the two authors of that book
     
  6. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #6
    Depends on the type of app you are making. Let me assure you that with the games we have developed that arc4random() does cause minor slowdowns in the FPS whereas random() barely touches it. For normal use though just use whatever you feel like at the time :p
     
  7. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.html
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    OK, just for fun I ran some timings of the three random number generators. I ran this on an iPod Touch 2nd generation running OS 3.0. The code was built with Release settings. I ran this several times with similar results. arc4random() is about 8 times slower than random() in this test.

    Code is below.

    Code:
    #define REPETITIONS (10 * 1000)
    
    - (void)testRandom
    {
    {
    	NSTimeInterval	start = [NSDate timeIntervalSinceReferenceDate];
    
    	for (int i = 0; i < REPETITIONS; i++)
    	{
    		int		j = rand();
    	}
    
    	NSTimeInterval	end = [NSDate timeIntervalSinceReferenceDate];
    
    	NSLog(@"rand(); %d repetitions", REPETITIONS);
    	NSLog(@"time = %g secs", (end - start) * 1000.0);
    }
    
    {
    	NSTimeInterval	start = [NSDate timeIntervalSinceReferenceDate];
    
    	for (int i = 0; i < REPETITIONS; i++)
    	{
    		int		j = random();
    	}
    
    	NSTimeInterval	end = [NSDate timeIntervalSinceReferenceDate];
    
    	NSLog(@"random(); %d repetitions", REPETITIONS);
    	NSLog(@"time = %g secs", (end - start) * 1000.0);
    }
    
    {
    	NSTimeInterval	start = [NSDate timeIntervalSinceReferenceDate];
    
    	for (int i = 0; i < REPETITIONS; i++)
    	{
    		int		j = arc4random();
    	}
    
    	NSTimeInterval	end = [NSDate timeIntervalSinceReferenceDate];
    
    	NSLog(@"arc4random(); %d repetitions", REPETITIONS);
    	NSLog(@"time = %g secs", (end - start) * 1000.0);
    }
    
    
    }
    
     
  9. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #9
    so the lesson here is to use the random number generator method you're use to unless you need to execute a whole lot of random numbers, or are ultra considerate about processing time for just a handful of numbers, in which case random() would be best.
     
  10. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #10
    does rand() or random() work faster if they haven't been seeded?

    also i remember reading that rand() and random() will end up getting you the same pattern of numbers over time unles you keep seeding, so surley when thats taken into account that could affect the results?

    (not trying to be like "OMG arc4radnom() rules" but im just saying what im thinking and hopefully you'll show me why im wrong :D and i learn more)
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    No. The way they work is they read the seed value and do a calcualation. The result of the calculation is saved in the seed value. Then the result of the calculation is returned as the function result. The seed value is a 32bit integer. The seed is set to 1 when the c library starts up. So there's always a seed.

    Since the seed value is reused for every calculation if it ever returns to the start value, 1 or whatever you set it to, then the pattern will repeat. It's a PSEUDO-random number generator. This can certainly 'affect the results' but whether it's important for your code depends on how many calls you make to the RNG and what you use the results for. It can also be a useful feature of the RNG if you want to test the same code over and over again. Even though you're getting random numbers you can get the same series of random numbers every time.
     

Share This Page