Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 29, 2010, 04:31 PM   #1
chainprayer
macrumors 6502a
 
Join Date: Feb 2008
Send a message via AIM to chainprayer
Random int not so random

I have a switch set up to select random values like this...

Code:
- (IBAction)randomResultGenerator {
	int rNumber = rand() % 20;
	
	switch (rNumber) {
			
	case 0:
	result.text = @"A.J.";
	break;
		
	case 1:
	result.text = @"Abacus";
	break;

etc...
Every time I launch my app it runs through the same order. Is there something I am doing wrong to generate a random number? They run in random order, but its the same random order every time.
__________________
MemeFire.com - You can has LOL
chainprayer is offline   0 Reply With Quote
Old May 29, 2010, 04:40 PM   #2
robbieduncan
Demi-God (Moderator)
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
You haven't seeded the random number generator.
robbieduncan is offline   0 Reply With Quote
Old May 29, 2010, 04:51 PM   #3
Bill McEnaney
macrumors 6502
 
Join Date: Apr 2010
Why not try the srand() function?
Bill McEnaney is offline   0 Reply With Quote
Old May 29, 2010, 06:49 PM   #4
TiberiusXavier
macrumors member
 
Join Date: Apr 2010
Location: Chicago
Random numbers do not exist in programs. What is offered instead is a pseudo-random algorithm or function rand that generates a vast range of distribution values. This rand function is fully deterministic given the input value. This input value begins with the seeded value (0 by default) and is set by another function called srand. Most programmers attain another level of pseudo-random sequence by seeding the function using the system clock (theoretically never the same value for the lifespan of the application or by then it will never be noticeable.) This seeding is done like srand( time( 0 ) );.

Next is the distribution of this randomness. The quick (both CPU cyclewise and coding) is to modulo the return value of rand by the maximum value (and 0 being the minimum.) However, to get a uniform distribution over the range (the quick approach favors lower numbers), it would require a floating-point math operation of dividing the return value of rand by the MAX_RAND constant (which is platform specific!)

So to get a random number from minimum to maximum inclusively:
Quick:
Code:
srand( time( 0 ) );  // Do once at start-up and never again.
int randomInt = minimum + rand() % ( maximum - minimum + 1 );  // Integer between 0 and max
Slow:
Code:
srand( time( 0 ) );  // Do once at start-up and never again.
int randomInt = minimum + (int)( ( maximum - minimum + 1) * ( rand() / ( 1.0 + (double)RAND_MAX ) ) );
TiberiusXavier is offline   0 Reply With Quote
Old May 29, 2010, 10:32 PM   #5
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
If you want random numbers everytime just use arc4random()
__________________
2.4 MacBook Aluminum 480GB SSD/750GB Optibay 8GB RAM CE | MacBook 2.4 SR 4GB, 320GB HD | PB G4 1.67 | 15 TiBook-DVI 800
iPhone 4S 16GB | iPhone 5 32GB
KoolStar is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone 4 ringer not working Causality iPhone Tips, Help and Troubleshooting 3 Oct 20, 2012 01:07 AM
Youtube not working in Safari? Hyde244 Mac Applications and Mac App Store 12 Nov 16, 2011 11:12 PM
A random download from a not so random site? vevierta Mac OS X 1 Dec 18, 2010 06:39 AM
Random play not so random? Eric S. iPod 2 Dec 22, 2009 03:47 PM
Random play not so random Mark16q iPod 0 Jan 30, 2008 01:52 PM


All times are GMT -5. The time now is 02:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC