Skip to content

Commit b50a678

Browse files
authored
Merge pull request MicrosoftDocs#3647 from TylerMSFT/twhitney-github3202
fix sample code per github 3202
2 parents f4a52da + 78ad8c7 commit b50a678

File tree

1 file changed

+70
-60
lines changed
  • docs/c-runtime-library/reference

1 file changed

+70
-60
lines changed
+70-60
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "rand"
3-
description: "API reference for rand, which generates a pseudorandom number by using a well-known and fully-reproducible algorithm."
4-
ms.date: "4/2/2020"
3+
description: "API reference for rand, which generates a pseudorandom number by using a well-known and fully reproducible algorithm."
4+
ms.date: "7/7/2021"
55
api_name: ["rand", "_o_rand"]
66
api_location: ["msvcrt.dll", "msvcr80.dll", "msvcr90.dll", "msvcr100.dll", "msvcr100_clr0400.dll", "msvcr110.dll", "msvcr110_clr0400.dll", "msvcr120.dll", "msvcr120_clr0400.dll", "ucrtbase.dll", "api-ms-win-crt-utility-l1-1-0.dll", "ntoskrnl.exe", "api-ms-win-crt-private-l1-1-0.dll"]
77
api_type: ["DLLExport"]
@@ -11,12 +11,12 @@ helpviewer_keywords: ["generating pseudorandom numbers", "random numbers, genera
1111
---
1212
# `rand`
1313

14-
Generates a pseudorandom number by using a well-known and fully-reproducible algorithm. A more programmatically secure version of this function is available; see [`rand_s`](rand-s.md). Numbers generated by **`rand`** are not cryptographically secure. For more cryptographically secure random number generation, use [`rand_s`](rand-s.md) or the functions declared in the C++ Standard Library in [`<random>`](../../standard-library/random.md).
14+
Generates a pseudorandom number. A more programmatically secure version of this function is available; see [`rand_s`](rand-s.md). Numbers generated by **`rand`** aren't cryptographically secure. For more cryptographically secure random number generation, use [`rand_s`](rand-s.md) or the functions declared in the C++ Standard Library in [`<random>`](../../standard-library/random.md).
1515

1616
## Syntax
1717

1818
```C
19-
int rand( void );
19+
int rand(void);
2020
```
2121
2222
## Return Value
@@ -27,7 +27,7 @@ int rand( void );
2727
2828
The **`rand`** function returns a pseudorandom integer in the range 0 to **`RAND_MAX`** (32767). Use the [`srand`](srand.md) function to seed the pseudorandom-number generator before calling **`rand`**.
2929
30-
The **`rand`** function generates a well-known sequence and is not appropriate for use as a cryptographic function. For more cryptographically secure random number generation, use [`rand_s`](rand-s.md) or the functions declared in the C++ Standard Library in [`<random>`](../../standard-library/random.md). For information about what's wrong with **`rand`** and how `<random>` addresses these shortcomings, see this video entitled [rand Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful).
30+
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`](rand-s.md) or the functions declared in the C++ Standard Library in [`<random>`](../../standard-library/random.md). For information about what's wrong with **`rand`** and how `<random>` addresses these shortcomings, see this video entitled [rand Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful).
3131
3232
By default, this function's global state is scoped to the application. To change this, see [Global state in the CRT](../global-state.md).
3333
@@ -37,80 +37,90 @@ By default, this function's global state is scoped to the application. To change
3737
|-------------|---------------------|
3838
|**`rand`**|`<stdlib.h>`|
3939
40-
For additional compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
40+
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
4141
4242
## Example
4343
4444
```C
4545
// crt_rand.c
4646
// This program seeds the random-number generator
47-
// with the time, then exercises the rand function.
48-
//
47+
// with a fixed seed, then exercises the rand function
48+
// to demonstrate generating random numbers, and
49+
// random numbers in a specified range.
4950
50-
#include <stdlib.h>
51-
#include <stdio.h>
52-
#include <time.h>
51+
#include <stdlib.h> // rand(), srand()
52+
#include <stdio.h> // printf()
5353
54-
void SimpleRandDemo( int n )
54+
void SimpleRandDemo(int n)
5555
{
56-
// Print n random numbers.
57-
int i;
58-
for( i = 0; i < n; i++ )
59-
printf( " %6d\n", rand() );
56+
// Print n random numbers.
57+
for (int i = 0; i < n; i++)
58+
{
59+
printf(" %6d\n", rand());
60+
}
6061
}
6162
62-
void RangedRandDemo( int range_min, int range_max, int n )
63+
void RangedRandDemo(int range_min, int range_max, int n)
6364
{
64-
// Generate random numbers in the half-closed interval
65-
// [range_min, range_max). In other words,
66-
// range_min <= random number < range_max
67-
int i;
68-
for ( i = 0; i < n; i++ )
69-
{
70-
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
71-
+ range_min;
72-
printf( " %6d\n", u);
73-
}
65+
// Generate random numbers in the interval [range_min, range_max], inclusive.
66+
67+
for (int i = 0; i < n; i++)
68+
{
69+
// Note: This method of generating random numbers in a range isn't suitable for
70+
// applications that require high quality random numbers.
71+
// rand() has a small output range [0,32767], making it unsuitable for
72+
// generating random numbers across a large range using the method below.
73+
// The approach below also may result in a non-uniform distribution.
74+
// More robust random number functionality is available in the C++ <random> header.
75+
// See https://docs.microsoft.com/cpp/standard-library/random
76+
int r = ((double)rand() / RAND_MAX) * (range_max - range_min) + range_min;
77+
printf(" %6d\n", r);
78+
}
7479
}
7580
76-
int main( void )
81+
int main(void)
7782
{
78-
// Seed the random-number generator with the current time so that
79-
// the numbers will be different every time we run.
80-
srand( (unsigned)time( NULL ) );
83+
// Seed the random-number generator with a fixed seed so that
84+
// the numbers will be the same every time we run.
85+
srand(1792);
8186
82-
SimpleRandDemo( 10 );
83-
printf("\n");
84-
RangedRandDemo( -100, 100, 10 );
85-
}
86-
```
87+
printf("Simple random number demo ====\n\n");
88+
SimpleRandDemo(10);
89+
printf("\nRandom number in a range demo ====\n\n");
90+
RangedRandDemo(-100, 100, 100000);
91+
}```
8792
8893
```Output
89-
22036
90-
18330
91-
11651
92-
27464
93-
18093
94-
3284
95-
11785
96-
14686
97-
11447
98-
11285
99-
100-
74
101-
48
102-
27
103-
65
104-
96
105-
64
106-
-5
107-
-42
108-
-55
109-
66
94+
Simple random number demo ====
95+
96+
5890
97+
1279
98+
19497
99+
1207
100+
11420
101+
3377
102+
15317
103+
29489
104+
9716
105+
23323
106+
107+
Random number in a range demo ====
108+
109+
-82
110+
-46
111+
50
112+
77
113+
-47
114+
32
115+
76
116+
-13
117+
-58
118+
90
110119
```
111120

112121
## See also
113122

114-
[Floating-Point Support](../../c-runtime-library/floating-point-support.md)<br/>
115-
[`srand`](srand.md)<br/>
116-
[`rand_s`](rand-s.md)<br/>
123+
[Floating-Point Support](../../c-runtime-library/floating-point-support.md)\
124+
[`srand`](srand.md)\
125+
[`rand_s`](rand-s.md)\
126+
[C++ `<random>` library](../../standard-library/random.md)

0 commit comments

Comments
 (0)