# CUDA pseudorandom number generator CURAND

**URL:** <https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393>\
**Category:** CUDA Programming and Performance\
**Created:** [July 7, 2011, 3:43pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393 "2011-07-07T15:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rit](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Rit](https://forums.developer.nvidia.com/u/Rit)\
**Post date:** [July 7, 2011, 3:43pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393/1 "2011-07-07T15:43:13Z")

</div>

Hello

I read the manual “CUDA Toolkit 4.0 CURAND Guide PG-05328-040\_v01 | January 2011”

From : [CUDA Toolkit Documentation](http://developer.nvidia.com/nvidia-gpu-computing-documentation)

When I compile the source from my PC so I have to change path of cuda.h and curand.h

```auto
/*

* This program uses the host CURAND API to generate 100

* pseudorandom floats .

*/

# include "stdio.h"

# include "stdlib.h"

# include "../include/cuda.h"

# include "../include/curand.h"

# define CUDA_CALL (x) do { if ((x) != cudaSuccess ) { \

	printf (" Error at %s:%d\n", __FILE__ , __LINE__ );\

	return EXIT_FAILURE ;}} while (0)

# define CURAND_CALL (x) do { if ((x) != CURAND_STATUS_SUCCESS ) { \

	printf (" Error at %s:%d\n", __FILE__ , __LINE__ ); \

	return EXIT_FAILURE ;}} while (0)

int main (int argc , char * argv [])

{

	size_t n = 100;

	size_t i;

	curandGenerator_t gen;

	float * devData , * hostData ;

	/* Allocate n floats on host */

	hostData = ( float *) calloc (n, sizeof ( float ));

	/* Allocate n floats on device */

	CUDA_CALL ( cudaMalloc (( void **)& devData , n * sizeof ( float )));	//HERE IS 26TH line

	/* Create pseudo - random number generator */

	CURAND_CALL ( curandCreateGenerator (& gen ,	CURAND_RNG_PSEUDO_DEFAULT ));

	/* Set seed */

	CURAND_CALL ( curandSetPseudoRandomGeneratorSeed (gen , 1234));	//HERE IS 32TH LINE

	/* Generate n floats on device */

	CURAND_CALL ( curandGenerateUniform (gen , devData , n));

	/* Copy device memory to host */

	CUDA_CALL ( cudaMemcpy ( hostData , devData , n * sizeof ( float ),

	cudaMemcpyDeviceToHost ));

	/* Show result */

	for(i = 0; i < n; i++) {

		printf ("%1.4 f ", hostData [i]);

	}

	printf ("\ n");

	/* Cleanup */

	CURAND_CALL ( curandDestroyGenerator (gen));

	CUDA_CALL ( cudaFree ( devData ));

	free ( hostData );

	return EXIT_SUCCESS ;

}

```

I wonder the result.

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\sarit>nvcc -arch=sm_13 c

udarand.cu

cudarand.cu

cudarand.cu(26): error: identifier "x" is undefined

cudarand.cu(26): error: expected a ";"

cudarand.cu(29): error: expected a ";"

cudarand.cu(32): error: expected a ";"

cudarand.cu(35): error: expected a ";"

cudarand.cu(38): error: expected a ";"

cudarand.cu(45): warning: unrecognized character escape sequence

cudarand.cu(48): error: expected a ";"

cudarand.cu(49): error: expected a ";"

cudarand.cu(19): warning: variable "gen" was declared but never referenced

cudarand.cu(20): warning: variable "devData" was declared but never referenced

8 errors detected in the compilation of "C:/Users/7-64/AppData/Local/Temp/tmpxft

_00000498_00000000-6_cudarand.cpp1.ii".

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\sarit>

```

Here is my nvcc version. (If need)

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\sarit>nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2011 NVIDIA Corporation

Built on Fri_May_13_01:41:59_PDT_2011

Cuda compilation tools, release 4.0, V0.2.1221

```

There is a “x” in the argument. So what does this mean ?

```auto
cudarand.cu(26): error: identifier "x" is undefined

```

I deleted “ULL” at 32th line, even it does not relevant to 8 erros.

The line number has been shortened in order to view in compact page.

Where should I start to figure out the problem ?

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [July 7, 2011, 4:29pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393/2 "2011-07-07T16:29:20Z")

</div>

It looks like something is going wrong with the interpretation of the preprocessor macros you have. (That’s where the “x” is coming from, I assume.) I’m not sure what the problem is beyond that.

---

<div class="post-metadata">

**Author:** ![Rit](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Rit](https://forums.developer.nvidia.com/u/Rit)\
**Post date:** [July 7, 2011, 6:42pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393/3 "2011-07-07T18:42:33Z")

</div>

> [@](#):
>
> It looks like something is going wrong with the interpretation of the preprocessor macros you have. (That’s where the “x” is coming from, I assume.) I’m not sure what the problem is beyond that.

Thank you for your reply.

Is there anyboy got this problem please let me know.

My OS = M$ 7-64.

MS visual studio professional 2008.

TwT

---

<div class="post-metadata">

**Author:** ![Rit](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Rit](https://forums.developer.nvidia.com/u/Rit)\
**Post date:** [July 7, 2011, 7:30pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393/4 "2011-07-07T19:30:16Z")

</div>

I take a look at my classic book, “Numerical Recipe in C”

After that I try to use ran2() on GPU.

Badluck,

```auto
"static" is not allowed within a __device__ or __global__ function

```

[External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wallbash.gif "Image hosted on another site. Click to open in a new tab.")

---

<div class="post-metadata">

**Author:** ![Rit](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Rit](https://forums.developer.nvidia.com/u/Rit)\
**Post date:** [July 7, 2011, 8:33pm UTC](https://forums.developer.nvidia.com/t/cuda-pseudorandom-number-generator-curand/23393/5 "2011-07-07T20:33:46Z")

</div>

I copy directory common from “CUDA by Example” and place in the toolkit

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0>dir

 Volume in drive C has no label.

 Volume Serial Number is F417-A0D2

Directory of C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0

07/08/2011 03:11 AM <DIR> .

07/08/2011 03:11 AM <DIR> ..

06/28/2011 03:01 PM <DIR> bin

07/08/2011 03:11 AM <DIR> common

06/28/2011 03:01 PM <DIR> computeprof

06/28/2011 03:01 PM <DIR> doc

06/28/2011 03:01 PM <DIR> extras

06/28/2011 03:01 PM <DIR> include

06/28/2011 03:01 PM <DIR> lib

06/28/2011 03:01 PM <DIR> open64

07/08/2011 03:20 AM <DIR> sarit

06/28/2011 03:01 PM <DIR> src

               0 File(s) 0 bytes

```

In directory common you will found file book.h as uaual.

Therefore I modify my #include and get rid of the CUDA\_CALL

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\sarit>nvcc ridda.cu

ridda.cu

tmpxft_00001a00_00000000-3_ridda.cudafe1.gpu

tmpxft_00001a00_00000000-8_ridda.cudafe2.gpu

ridda.cu

tmpxft_00001a00_00000000-3_ridda.cudafe1.cpp

tmpxft_00001a00_00000000-14_ridda.ii

tmpxft_00001a00_00000000-15_ridda.obj : error LNK2019: unresolved external symbo

l curandDestroyGenerator referenced in function main

tmpxft_00001a00_00000000-15_ridda.obj : error LNK2019: unresolved external symbo

l curandGenerateUniform referenced in function main

tmpxft_00001a00_00000000-15_ridda.obj : error LNK2019: unresolved external symbo

l curandSetPseudoRandomGeneratorSeed referenced in function main

tmpxft_00001a00_00000000-15_ridda.obj : error LNK2019: unresolved external symbo

l curandCreateGenerator referenced in function main

a.exe : fatal error LNK1120: 4 unresolved externals

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\sarit>

```

[External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wallbash.gif "Image hosted on another site. Click to open in a new tab.")
