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

HarryWorksInc

macrumors regular
Original poster
Feb 21, 2010
179
0
Does anyone know of a way to round integers?

I have a button to calculate a random integer out of 60 and then i divide it by 2 and I need it to be a whole number.
 
What types are the variables?

If you're using float or double, don't do that; use int instead.

???

Do you mean type conversion from a native float type (float, double) to a native int type (int, long)? That truncates (of course, even as I say this, I haven't tested it with Objective-C, only C, C++, and Java, however since Objective-C is a superset of C, I would expect C rules to work)

Code:
float f;
f = 3.14159

int i;
i = f;
// i now contains 3

i = (3.0*f);
// i now contains 9

If you really want this to round, try this code instead
Code:
i = f + 0.5;
// i now contains 3

i = (3.0*f)+0.5;
// i now contains 10

NSNumber also accepts values and stores them, and these objects can be accessed as integers, but there's no indication that "rounding" occurs.

RonC
 
Does anyone know of a way to round integers?

I have a button to calculate a random integer out of 60 and then i divide it by 2 and I need it to be a whole number.

I read this and I see:
a) Select a random integer in 1..60.
b) Divide that random integer by 2 NOTE: result must be an integer.

There are 2 simple ways to implement this. In both cases, the simplest thing is to use int's as the datatype, since only int's are used in your requirements.

However, there is a missing requirement: what do you want to do with odd "random integers". Use 31 as an example odd random integer. There are 2 integer values that resemble 31/2 - 15 or 16 - since 31/2 = 15.5. Which do you want?

Option 1 is to simply use int types for everything. Divide by 2 returns an integer.

That's simple code:
Code:
int num;
num = pickRandomInteger(60); // Imagine this function does the random selection
num /= 2; // Done. Eg: 30 -> 15, 31 -> 15

Option 2:
Ok. But now let's say you want to ROUND that number up.

Example: Number picked is 31. Divide by 2 is 16 (not 15.5).

This code is simple, but a brief explanation follows:
Code:
int num;
num = pickRandomInteger(60); // Imagine this function does the random selection
num = (num+1)/2; // Done. Eg: 30 -> 15, 31 -> 16
Here's what happens. Remember that C integer division truncates instead of rounds. Because of that, any non-integer remainers are simply dropped. The "add 1" is similar to the 0.5 in my prior response, as what it does is make integer truncation look like rounding.
 
Well, I am making an app in which the user rolls a dice and moves their player accordingly. I have learned that random integer pickers will repeat after a certain amount of time, by having a random int out of 60 and dividing it by 2 it gives me a longer cycle of random integers before it starts over again.

That is how it differs.
 
I learned it by making a pair of dice and it would start to repeat after a while.

And the random int selector is:

int RandomInt = (rand() % 6) + 1;
 
How does this differ from

Code:
int num = pickRandomInteger(30);

?

It matches the requirements?

Does anyone know of a way to round integers?

I have a button to calculate a random integer out of 60 and then i divide it by 2 and I need it to be a whole number.

I read this and I see:
a) Select a random integer in 1..60.
b) Divide that random integer by 2 NOTE: result must be an integer.

I don't know why he wants to "calculate a random integer out of 60," but he does so that's the code I wrote.
 
It matches the requirements?

I don't know why he wants to "calculate a random integer out of 60," but he does so that's the code I wrote.

We aren't here to act as contractors for people who post specifications (most of us here get paid for that), we're here to help others learn to program applications for the iPhone OS.

By asking questions, dejo determined that the OP had been misinformed regarding random numbers, and that a good solution for a random number between (in this instance) 1 and 30 would be:

Code:
int num = arc4random() % 30 + 1;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.