Is there any way to use rand() function in cuda like the example i have attached

device unsigned int randomInt(unsigned int max)
{
return rand() % max;
}

No. rand() is a routine supplied by stdlib.h, and in general those routines are not usable in device code.

One method to generate random numbers on the device is to use the CURAND library.

[url]http://docs.nvidia.com/cuda/curand/index.html#axzz3QDzA53jE[/url]

There are also CUDA sample codes demonstrating random number generation using CURAND.

[url]http://docs.nvidia.com/cuda/cuda-samples/index.html#monte-carlo-estimation-of-pi--inline-prng-[/url]