Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created October 31, 2013 06:20
Show Gist options
  • Save ashwin/7245048 to your computer and use it in GitHub Desktop.
Save ashwin/7245048 to your computer and use it in GitHub Desktop.
How to generate random numbers using Thrust
#include <thrust/device_vector.h>
#include <thrust/random.h>
struct GenRand
{
__device__
float operator () (int idx)
{
thrust::default_random_engine randEng;
thrust::uniform_real_distribution<float> uniDist;
randEng.discard(idx);
return uniDist(randEng);
}
};
const int randNum = 1000; // Size of vector
thrust::device_vector<float> rVec(randNum);
thrust::transform(
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(randNum),
rVec.begin(),
GenRand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment