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 11-09-2011, 04:03 PM   #1 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Question Modifying code for creating random numbers

Hey,

I found the following method on the web but I do not want all these decimals places. I only want things like 5.2 or 7.4 etc...

How would I adjust this method to do that?

Code:
-(float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2
{
    int startVal = num1*10000;
    int endVal = num2*10000; 

    int randomValue = startVal + (arc4random() % (endVal - startVal));
    float a = randomValue;

    return (a / 10000.0);
}
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-09-2011, 04:39 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 Objective Zero View Post
Hey,

I found the following method on the web but I do not want all these decimals places. I only want things like 5.2 or 7.4 etc...

How would I adjust this method to do that?

Code:
-(float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2
{
    int startVal = num1*10000;
    int endVal = num2*10000; 

    int randomValue = startVal + (arc4random() % (endVal - startVal));
    float a = randomValue;

    return (a / 10000.0);
}

Because of the way computers represent floating point numbers in binary, a decimal number doesn't really have "a number of decimal places." The number is stored internally as a sum of fractions of powers of 2.

0.1, for example, is roughly 1/16+1/32+1/256+1/512+1/4096 (plus some even smaller fractions.)

If you use code like this

float num = 1.1
NSLog(@"num = %.9f", num);

You will get something like

"num = 1.099999999997"

If you want to display a number to a smaller number of decimal places, use string formatting to do it. Like:

NSString* numString = [NSString stringWithFormat:@"%.1f", num];

That will give you a string like 5.2 or 7.4 as you want.
__________________
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 11-09-2011, 08:42 PM   #3 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Hmm, maybe I have another issue at hand here. So I guess my real issue here is that I don't really care about the # of decimal places. Its just that, I just want arc4random to pick up the the tenth in the decimal places. So I only want it to choose things like 1.5,1.6,1.7. I do not want it picking numbers like 1.567, 1.832.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 10:59 AM   #4 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

Quote:
Originally Posted by Objective Zero View Post
Hmm, maybe I have another issue at hand here. So I guess my real issue here is that I don't really care about the # of decimal places. Its just that, I just want arc4random to pick up the the tenth in the decimal places. So I only want it to choose things like 1.5,1.6,1.7. I do not want it picking numbers like 1.567, 1.832.
Actually Duncan already gave u the answer. I'll modify it further in case you don't get it.

Code:
float num = [self randomFloatBetween:5.4 andLargerFloat:10.6];
NSString* numString = [NSString stringWithFormat:@"%.1f", num];
this will give u 5.6, 5.8 etc, but in NSString format. You cannot have exactly 5.6 in number/float because a float is a number that floats. you give it a value of 5.6, it will actually hold 5.600000000231, or 5.5999999213.

Hope that helps.
rocotilos 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: 404
17 members and 387 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, chiataytuday, dre, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, mbadegree, n00b, pbart, Retouchable, Sami Gh, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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