How to invert a range - numbers

I have a range of values from 0 to 100, and I want to have the values inverted (ex. 100->0,0->100, 25->75, etc)
How would I do this? (in any language)

For each value, do 100 - value.
Note:
A general function for a range (min, max) would be:
invert(min, max, value)
=> max - value + min;

Related

OR-Tools: An IntVar that is one IntVar minus another IntVar in Java?

I'm using CP-SAT Solver in Java, and my objective is to minimize the range in the max and min values of an array of IntVars.
Creating IntVars for the max and min values in the array in Java is no problem. The challenge is creating the range IntVar in Java. In Python it's simple:
range = model.NewIntVar(1, 1000, "range")
model.Add(range == max - min)
model.Minimize(range)
I know that with OR-Tools in Java I need to use model.addEquality, instead of ==. But how to accomplish the subtraction to get the 'range' IntVar? I thought of using LinearExpr.sum on an IntVar[] containing max and the negative of min, where the negative of min comes from: LinearExpr.term(min, -1). But LinearExpr.term returns a LinearExpr, and LinearExpr.sum only accepts arrays of IntVars.
How can this be done in Java?
The cumbersome java way :-)
model.AddEquality(LinearExpr.Sum(new IntVar[] {x, y}), 1);
This is shown in the rabbit + pheasant sample.

What is the min and max value for a double field in MongoDB?

I need to find out what is the min and max value for a double field in mongoDB, including the number of precision for it.
I found this link: http://bsonspec.org/spec.html
Because MongoDB uses BSon, I'm looking for the BSON information. There is says:
double 8 bytes (64-bit IEEE 754-2008 binary floating point)
But how do I calculate the min and max number based on that?
I ended up at this link: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
But couldn't understand how to calculate the min and max values.
The wikipedia link you include has the precise answer:
min: -2^1023 * (1 + (1 − 2^−52)) (approx: -1.7976931348623157 * 10^308)
max: 2^1023 * (1 + (1 − 2^−52)) (approx: 1.7976931348623157 * 10^308)

Perl: getting count for range of values

I want to perform a count for a range of values, i.e, I have 900 values of X between 1 to 75x10^6. I need to count the number of times these X's fall in range like 1-1000000, 1000001-2000000, 2000001-3000000 ... 750 ranges, then return the counts of these ranges.
I have the values of X stored in an array so I could have done it with for loop and if..else, but giving 750 if-else's is no solution and I don't know how to implement value range in hash-keys. Please help
Thank you in advance :)
For each value, you can subtract 1, divide by 1000000, and cut off any decimals. That gives you the index of the range as a number between 0 and 749 (inclusive).
Example:
use strict;
use warnings;
my #values = (...); # filled from somewhere
my #range_count;
for my $value (#values) {
my $x = int(($value - 1) / 1e6);
$range_count[$x]++;
}
Now $range_count[0] contains the number of values in the first range, $range_count[1] the number of values in the second range, etc.
However, if there were no values in some range, the count will be undef, not 0. If this difference is important, define #range_count as
my #range_count = (0) x 750;
instead.

High-precision random numbers on iOS

I have been trying this for a while but thus far haven't had any luck.
What is the easiest way to retrieve a random number between two very precise numbers on iOS?
For example, I want a random number between 41.37783830549337 and 41.377730629131634, how would I accomplish this?
Thank you so much in advance!
Edit: I tried this:
double min = 41.37783830549337;
double max = 41.377730629131634;
double test = ((double)rand() / RAND_MAX) * (max - min) + min;
NSLog(#"Min:%lf, max:%lf, result:%lf",min,max,test);
But the results weren't quite as precise as I was hoping, and ended up like this::
Min:41.377838, max:41.377731, result:41.377838
You can normalise the output of rand to any range you want:
((double)rand() / RAND_MAX) * (max - min) + min
[Note: This is pure C, I'm assuming it works equivalently in Obj-C.]
[Note 2: Replace double with the data-type of your choice as appropriate.]
[Note 3: Replace rand with the random-number source of your choice as appropriate.]

choose random value

I am new to iPhone programming. I have 10 number say (1,2,3,4,5,6,7,8,9,10). I want to choose randomly 1 number from the above 10 numbers. How can I choose a random number from a set of numbers?
If you simply want a value between 1 and 10, you can use the standard C rand() method. This returns an integer between zero and RAND_MAX.
To get a value between 0 and 9 you can use the % operator. So to get a value between 1 and 10 you can use:
rand()%10 + 1
If you don't want the same series of pseudo random numbers each time, you'll need to use srand to seed the random number generator. A good value to seed it with would be the current time.
If you're asking about choosing a number from a list of arbitrary (and possibly non consecutive) numbers, you could use the following.
int numbers[] = {2,3,5,7,11,13,17,19,23,29};
int randomChoice = numbers[rand()%10];
To generate a random number you should use random() function. But if you call it twice it gives you two equal answers. Before calling random(), call srand(time()) to get fresh new random number. if you want to use for(int i = 0; ...) to create numbers,
use srand(time() + i).
Something like this:
- (IBAction)generate:(id)sender
{
// Generate a number between 1 and 10 inclusive
int generated;
generated = (random() % 10) + 1;
}