# Calculate nearest number?

• 02-20-2003
electrolove
Calculate nearest number?
How to do this?

Example: I have a couple of numbers: 16, 32, 48, 64, 80, 96 and so on, they increase by 16 each time.

How can I change a random number to the nearest of these numbers?

In this example the random number is 22 and 16 is the nearest number, so my answer is 16. A couple of more examples: 33=32, 61=64, 17=16

How to do this in code in a simple way?
• 02-20-2003
Monster
int i = 39;

i = (i+8)&-16;
• 02-20-2003
Magos
Code:

```int NearestNumber(int Number) {   return (((int)((Number + 8) / 16)) * 16); }```
• 02-20-2003
Magos
Do you want the random number to be one of 16,32,48 etc... ? Then it's easier to generate it this way:
Code:

`int Number = (rand() % RangeOfRandomNumbers) * 16;`
RangeOfRandomNumbers is how many 16-tuples to choose from, ie: 5 gives 0,16,32,48,64.
• 02-20-2003
electrolove
Quote:

Originally posted by Magos
Do you want the random number to be one of 16,32,48 etc... ? Then it's easier to generate it this way:
Code:

`int Number = (rand() % RangeOfRandomNumbers) * 16;`
RangeOfRandomNumbers is how many 16-tuples to choose from, ie: 5 gives 0,16,32,48,64.

No, I don't need random numbers, thanks anyway :-)
• 02-20-2003
electrolove
Quote:

Originally posted by Monster
int i = 39;

i = (i+8)&-16;

This code seems nice for gaps of 16.

Can I use this code idea even if the gap between numbers is something else then 16? Example:

20, 40, 60, 80

or

3, 6, 9, 12

:p
• 02-20-2003
Magos
Quote:

Originally posted by electrolove
This code seems nice for gaps of 16.

Can I use this code idea even if the gap between numbers is something else then 16? Example:

20, 40, 60, 80

or

3, 6, 9, 12

:p

Ah, now I get what Monster was doing. He was cutting the last 4 bits in the number making it a 16 tuple. -16 is the same bitmask as 240.
Sorry, it only works for numbers like 2,4,8,16,32,64 etc...
• 02-20-2003
electrolove
Quote:

Originally posted by Magos
Ah, now I get what Monster was doing. He was cutting the last 4 bits in the number making it a 16 tuple. -16 is the same bitmask as 240.
Sorry, it only works for numbers like 2,4,8,16,32,64 etc...

Yea, I realize now that my example of a gap of 16 was not good. The thing I need is a function that takes the gap as parameter 1 and the value to be calculated as parameter 2. In that way I can use any gap to calculate the random number, that is what I need.

Anyone? :o
• 02-20-2003
Magos
Just modify my function above to:
Code:

```int NearestNumber(int Number, int Gap) {   return (((int)((Number + (int)(Gap / 2)) / Gap)) * Gap); }```
• 02-21-2003
electrolove
Quote:

Originally posted by Magos
Just modify my function above to:
Code:

```int NearestNumber(int Number, int Gap) {   return (((int)((Number + (int)(Gap / 2)) / Gap)) * Gap); }```

I have tested the code now and it does exactly the thing I want (as far as I know)

Thanks Magos :D
• 02-21-2003
Priyank
Hey in simple lingo....u cd even try this ...

Divide the random no. by 16.....ok get the remainder now if that is gr8r than 8 then the nearest no. is 16*(quotient+1) otherwise the no. is just (16*quotient).....give it a try and let me know if it works