'Generating random number within Cuda kernel in a varying range
I am trying to generate random number random numbers within the cuda kernel. I wish to generate the random numbers from uniform distribution and in the integer form, starting from 1 up to 8. The random numbers would be different for each of the threads. The range up to which random number can be generated would also vary from one thread to another. The maximum of the range in one thread might be as low as 2 or in the other thread it can be high as 8, but not higher than that. So, I am providing an example below of how I want the numbers to get generated :
In thread#1 --> maximum of the range is 2 and so the random number should be between 1 and 2
In thread#2 --> maximum of the range is 6 and so the random number should be between 1 and 6
In thread#3 --> maximum of the range is 5 and so the random number should be between 1 and 5
and so on...
Solution 1:[1]
@Robert's example doesn't generate a perfectly uniform distribution (although all the numbers in the range are generated and all the generated numbers are in the range). Both the smallest and largest value have 0.5 the probability of being chosen of the rest of the numbers in the range.
At step 2, you should multiply with the number of values in the range: (largest value - smallest value + 0.999999). *
At step 3, the offset should be (+ smallest value) instead of (+ smallest value + 0.5).
Steps 1 and 4 remain the same.
*As @Kamil Czerski noted, 1.0 is included in the distribution. Adding 1.0 instead of 0.99999 would sometimes result in a number outside of the desired range.
Solution 2:[2]
For a safer general purpose random integer function using curand_uniform() that can handle larger integers:
#include <math.h>
int rand = (int)(ceil((curand_uniform(&state)*(RANGE + 1))) - 1);
Multiple your float by RANGE + 1 then take the ceiling, subtract by 1, and cast as an integer. Taking the ceiling produces a whole number between 1 and RANGE + 1 so when we subtract by one we get an integer between 0 and RANGE.
Addition discussion:
If 0.0 were included in curand_uniform() and 1.0 were not then,
(int)((curand_uniform(&state)*(RANGE + 1)));
would produce an integer between 0 and RANGE. We are safe truncating to an integer because RANGE + 1 is not a possible result. We are also happy because the distribution includes our entire range.
Since 0.0 is excluded and 1.0 included then all possible results need to be shifted down by some amount to truncate to an integer safely. This is accomplished by adding .999999 to RANGE and multiplying.
(int)((curand_uniform(&state)*(RANGE + .999999)))
The solution is not perfect however because not all possible values between 0 and RANGE are represented (not considering 0 or RANGE). This produces a slight bias against the greatest integer in our range.
The greatest offset according to IEEE 754 Floating Point is .999999940395355224609375 as this would be the largest decimal less than one before the computer rounds up. The problem with using this value is that the computer will start rounding up for values greater than 1 when the decimal part exceeds approximately .999999. In fact, our offset must shrink in proportion to the value of our integer because the integer part takes up more space in memory. For integers greater than 10000000 you would have to amend the solution since virtually all decimal parts will round up.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | lefunction |
