Contenu principal

gpucoder.rng

R2026b

Control random number generator in generated GPU code

Since R2026b

    Description

    gpucoder.rng initializes the random number generator in generated GPU code by using the NVIDIA® cuRAND library. When you generate GPU code from gpucoder.rng, the generated code initializes the generator and uses it for subsequent calls to gpucoder.rand, gpucoder.randi, and gpucoder.randn. Use this function to reproduce sequences of random numbers or generate different sequences of random numbers. For more information, see Reproducibility.

    If you call gpucoder.rng in MATLAB®, gpucoder.rng initializes the MATLAB random number generator by using the rng function.

    Note

    Because the NVIDIA cuRAND library and MATLAB use different random number generators, the random numbers produced in MATLAB for gpucoder.rand, gpucoder.randn, and gpucoder.randi do not match the random numbers produced by generated GPU code.

    gpucoder.rng("default") initializes the random number generator with the default seed of 0. In generated GPU code, the random number generator uses the CURAND_RNG_PSEUDO_XORWOW generator type. For more information, see the cuRAND documentation on the NVIDIA website.

    example

    gpucoder.rng(seed) sets the seed of the random number generator.

    example

    Examples

    collapse all

    Create a MATLAB function, randUsingDefault, that generates random numbers by using gpucoder.rand. To use the default random number generator seed in subsequent calls to GPU Coder™ random number generation functions, use the "default" argument.

    function Y = randUsingDefault(n)
    gpucoder.rng("default");
    Y = gpucoder.rand(n);
    end

    Generate GPU code from randUsingDefault.

    cfg = coder.gpuConfig("mex");
    codegen randUsingDefault.m -config cfg -args {200}

    Generate a 4-by-4 matrix by using the generated MEX function randUsingDefault_mex.

    Y = randUsingDefault_mex(4)
    Y = 4×4
    0.4385	0.0530	0.4822	0.5126
    0.4604	0.3377	0.0428	0.2643
    0.2502	0.3968	0.5084	0.0520
    0.4947	0.8744	0.6545	0.5790

    Create a MATLAB function named randnUsingSeed that generates a matrix by using the gpucoder.randn function. If the resetGenerator input argument is true, reset the random number generator seed to 2.

    function Y = randnUsingSeed(sz1,sz2,resetGenerator)
    if resetGenerator
        gpucoder.rng(2);
    end
    Y = gpucoder.randn(sz1,sz2);
    end

    Generate GPU code from randnUsingSeed.

    cfg = coder.gpuConfig("mex");
    codegen randnUsingSeed.m -config cfg -args {0,0,false}

    Generate a 5-by-4 matrix by using the generated MEX function randnUsingSeed_mex. Specify the resetGenerator argument as true.

    Y = randnUsingSeed_mex(5,4,true)
    Y = 5×4
    -1.3322	0.2694	-1.5536	1.9227
    0.1524	-2.3717	-0.2757	0.3150
    -0.2752	1.1509	1.8704	0.9156
    0.3052	-0.2633	-0.3970	-0.7013
    -0.1619	2.0606	1.0849	0.1884

    Generate another 5-by-4 matrix without resetting the generator. The output is different.

    Y = randnUsingSeed_mex(5,4,false)
    Y = 5×4
    -1.2467	0.6683	0.2895	-0.3010
    -1.3132	0.3259	-1.2485	0.3757
    0.9530	0.6921	0.0877	1.8518
    -0.8706	0.4449	1.1088	-1.3921
    -1.0971	-0.8106	1.1149	-1.1833

    To repeat the sequence of random numbers, call randnUsingSeed_mex and reset the generator. The output matches the first matrix generated by randnUsingSeed_mex.

    Y = randnUsingSeed_mex(5,4,true)
    Y = 5×4
    -1.3322	0.2694	-1.5536	1.9227
    0.1524	-2.3717	-0.2757	0.3150
    -0.2752	1.1509	1.8704	0.9156
    0.3052	-0.2633	-0.3970	-0.7013
    -0.1619	2.0606	1.0849	0.1884

    Input Arguments

    collapse all

    Random number seed, specified as a nonnegative integer less than 2^32 or as "shuffle".

    • Specify seed as an integer, such as 1, to use that seed for the random number generator.

    • Specify seed as "shuffle" to set the seed based on the current time.

    Note

    Using gpucoder.rng with the "shuffle" option sets the seed by using the current time in seconds. To set a new seed with the "shuffle" option, use gpucoder.rng at least one second after the last call to gpucoder.rng("shuffle").

    Example: gpucoder.rng(23)

    Example: gpucoder.rng("shuffle")

    More About

    collapse all

    Extended Capabilities

    expand all

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2026b