# Rounding Integers

Discussion in 'iOS Programming' started by HarryWorksInc, Mar 8, 2010.

1. ### HarryWorksInc macrumors regular

Joined:
Feb 21, 2010
#1
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.

2. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Brobdingnag
#2
What types are the variables?

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

3. ### RonC macrumors regular

Joined:
Oct 18, 2007
Location:
Chicago-area
#3
???

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

4. ### RonC macrumors regular

Joined:
Oct 18, 2007
Location:
Chicago-area
#4
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.

Joined:
Feb 21, 2010
6. ### jnic macrumors 6502a

Joined:
Oct 24, 2008
Location:
Cambridge
#6
How does this differ from

Code:
`int num = pickRandomInteger(30);`
?

7. ### kyzen macrumors regular

Joined:
Feb 8, 2010
Location:
#7

good question

8. ### HarryWorksInc thread starter macrumors regular

Joined:
Feb 21, 2010
#8
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.

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#9
Where did you learn this? Also, what random number generator function are you using?

10. ### HarryWorksInc thread starter macrumors regular

Joined:
Feb 21, 2010
#10
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;

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#11
From the docs:
I believe that arc4random() is even better since it has twice the range of rand() and random().

12. ### RonC macrumors regular

Joined:
Oct 18, 2007
Location:
Chicago-area
#12
damn quote mechanism. check the next message.

13. ### RonC macrumors regular

Joined:
Oct 18, 2007
Location:
Chicago-area
#13
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.

14. ### jnic macrumors 6502a

Joined:
Oct 24, 2008
Location:
Cambridge
#14
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;`

Joined:
Apr 18, 2007
Location:
California