I need to generate a vector with random numbers between 0.0
and 1.0
using Thrust
. The only documented example I could find produces very large random numbers (thrust::generate(myvector.begin(), myvector.end(), rand
).
I'm sure the answer is simple, but I would appreciate any suggestions.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Thrust has random generators you can use to produce sequences of random numbers. To use them with a device vector you will need to create a functor which returns a different element of the random generator sequence. The most straightforward way to do this is using a transformation of a counting iterator. A very simple complete example (in this case generating random single precision numbers between 1.0 and 2.0) could look like:
In this example, the functor
prg
takes the lower and upper bounds of the random number as an argument, with(0.f,1.f)
as the default. Note that in order to have a different vector each time you call the transform operation, you should used a counting iterator initialised to a different starting value.There are already satisfactory answers to this questions. In particular, the OP and Robert Crovella have dealt with
thrust::generate
while talonmies has proposed usingthrust::transform
.I think there is another possibility, namely, using
thrust::for_each
, so I'm posting a fully worked example using such a primitive, just for the record.I'm also timing the different solutions.
THE CODE
On a laptop
Core i5
platform, I had the following timingsThe timings are equivalent, apart from the second one which uses
Thrust
's internal random number generator instead ofrand()
.Please, note that, differently from the other solutions, the one
thrust::generate
is somewhat more rigid since the function used to generate the random numbers cannot have input parameters. So, for example, it is not possible to scale the input arguments by a constant.The approach suggested by @talonmies has a number of useful characteristics. Here's another approach that mimics the example you quoted:
similar to the example you quoted, this uses rand(), and therefore can only be used to generate host vectors. Likewise it will produce the same sequence each time unless you re-seed rand() appropriately.
It might not be a direct answer to your question but, cuRand library is quite powerful in this concept. You may both generate random numbers at GPU and CPU, and it contains many distribution functions (normal distribution etc).
Search for the title: "An NVIDIA CURAND implementation" on this link: http://adnanboz.wordpress.com/tag/nvidia-curand/
One note is that, do not generate the generator again and again, it makes some precalculations. Calling curandGenerateUniform is quite fast and produces values between 0.0 and 1.0.