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.


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