I currently have a method to attempt to get a random float between two numbers that I provide however, it does not work properly.
For example if I feed in 4.0 as the lower float and 4.5 as the higher float, I get results that are like 0.8 or 1.2 and sometimes it is between those two numbers. So this leads me to believe that this method is no good. Here is the method:
- (float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2 {
return ((float)arc4random() / ARC4RANDOM_MAX) * num2-num1 + num1;
}
This is ARC4RANDOM_MAX
#define ARC4RANDOM_MAX 0x100000000
Anyway, what is causing this and how can I fix this?
Thanks!
Your code means this:
return (((float)arc4random() / ARC4RANDOM_MAX) * num2) - num1 + num1;
You need parentheses around num2-num1:
return ((float)arc4random() / ARC4RANDOM_MAX) * (num2-num1) + num1;
Related
I am trying to build a degrees/radians calculator. Here are the functions I have.
func degreesToRadians(degrees: Double) -> Double {
return degrees * (M_PI / 180)
}
func radiansToDegrees(radians: Double) -> Double {
return radians * (180 / M_PI)
}
degreesToRadians(90)
radiansToDegrees(M_PI/4)
degreesToRadians(90) returns 1.5707963267949
radiansToDegrees(M_PI/4) returns 45.0
What I want to happen is in the Degrees to Radians function instead of 1.5707963267949 I want the output to be π/2. Is this even possible?
Thanks.
To represent 90 degrees as π/2, what you want to do is
consider the fraction of degrees over 180 (i.e. numerator of degrees and denominator of 180);
reduce this fraction (i.e. divide both the numerator and denominator by the greatest common factor); in the case of 90/180, the greatest common factor is, in fact, 90, yielding a reduced numerator of 1 and a reduced denominator of 2;
the result as a string representation of the fraction, i.e. something like
"\(reducedNumerator)π/\(reducedDenominator)"
Obviously, if either the numerator or denominator are 1, then you can suppress that portion of the string, but hopefully you get the basic idea.
Take a crack at that.
If you want exactly: "π/2", I don't think this is possible with double. You might get this in String, but not in a number. I think your best option would be to take an optional bool in which case result is returned as multiple of pi.
degreesToRadians(90, resultAsMultipleOfPi:true)
If this bool is true then 0.5 should be returned. You may need to do some rounding off to get well rounded numbers.
If you look closely on what is really M_PI you will see that it is predefined double value in math.h that equals
#define M_PI 3.14159265358979323846264338327950288 /* pi */
If you want more precision you can declare and use long double value instead.
You can directly use M_PI constant.
If you need in float format just use this,
let pi = Float(M_PI)
#import "ViewControllerSettings.h"
#define DEGREES(radians) (radians * 180 / M_PI)
NSLog(#"%f", sinFita);
sinFita = asin(DEGREES(sinFita));
NSLog(#"%f", sinFita);
returns
2014-04-20 22:10:09.916 ---[8561:60b] 0.239580
2014-04-20 22:10:09.920 ---[8561:60b] nan
I require my answer to be in Degreesº, the and doubles are used.
The answer should be 13.86º
asin argument should be in radians and not degrees and the result returned by asin is also in radians so you will need to execute it as follows :
sinFita = DEGREES(asin(sinFita)); // Be aware that sinFita will now be in degrees and not in radians once this line is executed
Result is correct. Go through you calculation step by step. What do you think asin should return when the argument is greater than 1?
I am trying to add fractions to get a value to put in for my progress bar but am just getting 0. here is what I have and what it is logging.
NSLog(#"prog::%f",self.progView.progress);
int total = self.forms.count;
float calc = (float)(self.progView.progress / total) + (1/total);
NSLog(#"calc::%f",calc);
NSLog(#"total::%d",total);
self.progView.progress = calc;
NSLog(#"prog now:%f", self.progView.progress);
Log:
prog::0.000000
calc::0.000000
total::4
prog now:0.000000
Take care of where you place your cast to float. Try changing from this:
float calc = (float)(self.progView.progress / total) + (1/total);
To this:
float calc = ((float)self.progView.progress / (float)total) + (1/(float)total);
Alternatively, make total a float rather than an int, e.g.
float total = self.forms.count;
float calc = ((float)self.progView.progress / total) + (1 / total);
I am trying to make a simple objective-C height converter. The input is a (float) variable of feet, and I want to convert to (int) feet and (float) inches:
float totalHeight = 5.122222;
float myFeet = (int) totalHeight; //returns 5 feet
float myInches = (totalHeight % 12)*12; //should return 0.1222ft, which becomes 1.46in
However, I keep getting an error from xcode, and I realized that the modulo operator only works with (int) and (long). Can someone please recommend an alternative method? Thanks!
Even modulo works for float, use :
fmod()
You can use this way too...
float totalHeight = 5.122222;
float myFeet = (int) totalHeight; //returns 5 feet
float myInches = fmodf(totalHeight, myFeet);
NSLog(#"%f",myInches);
Why don't you use
CGFloat myInches = totalHeight - myFeet;
As answered earlier, subtracting is the way to go. Just remember to convert the one tenths of feet to inches by multiplying with 12:
float totalHeight = 5.122222;
int myFeet = (int) totalHeight;
float myInches = (totalHeight - myFeet) * 12;
I'm making a simple pong game. To make the ball move at the beginning of a new round, I am using
ballVelocity = CGPointMake(4 - arc4random() % 8,4 - arc4random() % 8);
However, the important part is just this:
4 - arc4random() % 8
However, there are a few problems with this: first and foremost, it doesn't really generate a random number. Only after I quit the simulator, then reopen it are new numbers generated. Secondly, I only want it to generate numbers between -4 and -2 or 2 and 4.
arc4random() is the preferred random function on the iphone, instead of rand(). arc4random() does not need seeding.
This code will generate the ranges you're interested in:
int minus2_to_minus4 = (arc4random() % 3) - 4;
int two_to_four = (arc4random() % 3) + 2;
You need to look at the rand() function. Basically, you "seed" it with a start value, and it returns a new random number every time you call it.
Or look at this question which has a full example using arc4random.
This will give you a floating point number between -4 and -2 OR 2 and 4
float low_bound = -4; //OR 2
float high_bound = -2;//OR 4
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
If you want a number in -4…-2 AND 2…4 try this:
float low_bound = 2;
float high_bound = 4;
float rndValueTemp = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
float rndValue = ((float)arc4random()/0x100000000)<0.5?-rndValueTemp:rndValueTemp;