Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

peacetrain67

macrumors member
Original poster
Dec 20, 2007
68
0
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?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

peacetrain67

macrumors member
Original poster
Dec 20, 2007
68
0
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.

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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

Kingbombs

macrumors member
Jun 24, 2009
98
0
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.

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
 

mccannmarc

macrumors 6502
Aug 15, 2008
270
0
Manchester, UK
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

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
 

Darkroom

Guest
Dec 15, 2006
2,445
0
Montréal, Canada
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

http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.html
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.

Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: rand(); 10000 repetitions
Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: time = 3.73298 secs
Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: random(); 10000 repetitions
Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: time = 1.44297 secs
Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: arc4random(); 10000 repetitions
Thu Sep 17 14:18:08 unknown testarc4random[141] <Warning>: time = 11.064 secs

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);
}


}
 

Darkroom

Guest
Dec 15, 2006
2,445
0
Montréal, Canada
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.
 

Kingbombs

macrumors member
Jun 24, 2009
98
0
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)
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
does rand() or random() work faster if they haven't been seeded?

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.

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?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.