Redigera

Dela via


rand

Generates a pseudorandom number. A more programmatically secure version of this function is available; see rand_s. Numbers generated by rand aren't cryptographically secure. For more cryptographically secure random number generation, use rand_s or the functions declared in the C++ Standard Library in <random>.

Syntax

int rand(void);

Return value

rand returns a pseudorandom number, as described above. There's no error return.

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767). Use the srand function to seed the pseudorandom-number generator before calling rand.

The rand function generates a well-known sequence and isn't appropriate for use as a cryptographic function. For more cryptographically secure random number generation, use rand_s or the functions declared in the C++ Standard Library in <random>.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
rand <stdlib.h>

For more compatibility information, see Compatibility.

Example

// crt_rand.c
// This program seeds the random-number generator
// with a fixed seed, then exercises the rand function
// to demonstrate generating random numbers, and
// random numbers in a specified range.

#include <stdlib.h> // rand(), srand()
#include <stdio.h> // printf()

void SimpleRandDemo(int n)
{
    // Print n random numbers.
    for (int i = 0; i < n; i++)
    {
        printf("  %6d\n", rand());
    }
}

void RangedRandDemo(int range_min, int range_max, int n)
{
    // Generate random numbers in the interval [range_min, range_max], inclusive.

    for (int i = 0; i < n; i++)
    {
        // Note: This method of generating random numbers in a range isn't suitable for
        // applications that require high quality random numbers.
        // rand() has a small output range [0,32767], making it unsuitable for
        // generating random numbers across a large range using the method below.
        // The approach below also may result in a non-uniform distribution.
        // More robust random number functionality is available in the C++ <random> header.
        // See https://learn.microsoft.com/cpp/standard-library/random
        int r = ((double)rand() / RAND_MAX) * (range_max - range_min) + range_min;
        printf("  %6d\n", r);
    }
}

int main(void)
{
    // Seed the random-number generator with a fixed seed so that
    // the numbers will be the same every time we run.
    srand(1792);

    printf("Simple random number demo ====\n\n");
    SimpleRandDemo(10);
    printf("\nRandom number in a range demo ====\n\n");
    RangedRandDemo(-100, 100, 100000);
}```

```Output
Simple random number demo ====

    5890
    1279
   19497
    1207
   11420
    3377
   15317
   29489
    9716
   23323

Random number in a range demo ====

     -82
     -46
      50
      77
     -47
      32
      76
     -13
     -58
      90

See also

Math and floating-point support
srand
rand_s
C++ <random> library