Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-01-2012, 02:17 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 63
jimmymacapps is on a distinguished road
Default Random number between two certain numbers

I am currently using:
Code:
25 + arc4random() % (436 - 25));
to pick a number between 25 and 455. I am now trying to now pick a number between 65 and 455. I have tried replacing the 25 at the start of the code to 65 but this sometimes gives me numbers greater than 455.

Thanks in advance for any help!!
jimmymacapps is offline   Reply With Quote
Old 02-01-2012, 02:44 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jimmymacapps View Post
I am currently using:
Code:
25 + arc4random() % (436 - 25));
to pick a number between 25 and 455. I am now trying to now pick a number between 65 and 455. I have tried replacing the 25 at the start of the code to 65 but this sometimes gives me numbers greater than 455.

Thanks in advance for any help!!
The code you posted will create random numbers between 25 and 435

In general, the code

Code:
int smallest = 25;
int largest = 500;
int random = smallest + arc4random() %(largest+1-smallest);
should give you a number in the range smallest ... largest

You could create a simple method to give you random numbers in a range:

Code:
- (unsigned long) randomFromSmallest: (unsigned long) smallest toLargest (unsigned long) largest
{
  int smallest = 25;
  int largest = 500;
  int random = smallest + arc4random() % (largest+1-smallest);
  return random;
}
And use it anyplace you need random numbers in a range.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-01-2012, 02:45 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
minimumValue + arc4random() % (maximumValue - minimumValue));
Edit: Duncan beat me to it, and remembered to add 1.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-01-2012, 02:56 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 63
jimmymacapps is on a distinguished road
Default

Thanks guys great answers! I used Duncans code and added one to my largest number!
Thanks again!
jimmymacapps is offline   Reply With Quote
Old 02-01-2012, 03:26 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Illinois
Posts: 44
GHuebner is on a distinguished road
Default

That is a great piece of code to throw in the Code Snippets Library

Quote:
Originally Posted by jimmymacapps View Post
Thanks guys great answers! I used Duncans code and added one to my largest number!
Thanks again!
GHuebner is offline   Reply With Quote
Old 02-01-2012, 11:30 PM   #6 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default

Great answer but I use a smaller method and it works just as fine. Can anyone tell me the diffrance?

In my game i generate random number between 1 and 10 and I use the code

teleportIndex = arc4random() % 10+1;

SOO.... wouldnt we just have to do

YOURVariable = arc4random() % 455+25;
2WeeksToGo is offline   Reply With Quote
Old 02-02-2012, 12:21 AM   #7 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

Quote:
Originally Posted by 2WeeksToGo View Post
Great answer but I use a smaller method and it works just as fine. Can anyone tell me the diffrance?

In my game i generate random number between 1 and 10 and I use the code

teleportIndex = arc4random() % 10+1;

SOO.... wouldnt we just have to do

YOURVariable = arc4random() % 455+25;
No.
This arc4random() % 455 will give you a number between 0 and 454. Adding 25 to it will give you a number between 25 and 479. But the OP needs a number between 25 and 455. That why you use what Duncan said.

If you think about it your are doing the same

Simply put where smallest = 1 and largest = 10 and you'll have

int random = 1 + arc4random() % (10+1-1);

which is

int random = 1 + arc4random() % 10;
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z

Last edited by apatsufas; 02-02-2012 at 12:24 AM.
apatsufas is offline   Reply With Quote
Old 02-02-2012, 02:32 AM   #8 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default

Quote:
Originally Posted by apatsufas View Post
No.
This arc4random() % 455 will give you a number between 0 and 454. Adding 25 to it will give you a number between 25 and 479. But the OP needs a number between 25 and 455. That why you use what Duncan said.

If you think about it your are doing the same

Simply put where smallest = 1 and largest = 10 and you'll have

int random = 1 + arc4random() % (10+1-1);

which is

int random = 1 + arc4random() % 10;
Yea but I can visualy see that the method I wrote actually works, it never genrates a number greater than 10. I have it right infront of me on a label and I have been testing my game like this for months, if it generated the number 11, i would know because the game would not produce a stage and that never happened.

I appriciate your reply but I dont get how your answer is valid since my way works 100% and I can see it.

I will look into duncans method for future coding but I just find this curious
2WeeksToGo is offline   Reply With Quote
Old 02-02-2012, 02:35 AM   #9 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default

Btw I think I know the problem.
when we do

variable = arc4random() % 10;

that generates a number from 0 to 9. so by adding 1 like
variable = arc4random() % 10 +1;

I move it all up so it now generates a number between 1 and 10. so YES if we add 25, it will just shift everything up so Duncans method is the only valid way to do this, I just got lucky with mine since my values start at 1.
2WeeksToGo is offline   Reply With Quote
Old 02-02-2012, 03:14 AM   #10 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

Quote:
Originally Posted by 2WeeksToGo View Post
Btw I think I know the problem.
when we do

variable = arc4random() % 10;

that generates a number from 0 to 9. so by adding 1 like
variable = arc4random() % 10 +1;

I move it all up so it now generates a number between 1 and 10. so YES if we add 25, it will just shift everything up so Duncans method is the only valid way to do this, I just got lucky with mine since my values start at 1.
Exactly. This % is the symbol for modulo. That's why arc4random() % 10 will give you numbers between 0 and 9.

If you read carefully through my previous post, you'll see that I'm saying that what you do works because you are using Duncans method.
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z

Last edited by apatsufas; 02-02-2012 at 03:16 AM.
apatsufas is offline   Reply With Quote
Old 02-02-2012, 02:09 PM   #11 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

it might be better if you write:

teleportIndex = 1 + arc4random()%10;

rather than:

teleportIndex = arc4random() % 10+1;

just because the human brain can more easily understand what its doing. But to the computer it makes no difference.
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 416
16 members and 400 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, PowerGoofy, QuantumDoja, Retouchable, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0