Skip to content

Commit a80b520

Browse files
TylerMSFTTylerMSFT
authored andcommitted
fix sample code
1 parent ae7b575 commit a80b520

File tree

1 file changed

+65
-54
lines changed
  • docs/c-runtime-library/reference

1 file changed

+65
-54
lines changed

docs/c-runtime-library/reference/rand.md

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Generates a pseudorandom number by using a well-known and fully-reproducible alg
1616
## Syntax
1717

1818
```C
19-
int rand( void );
19+
int rand(void);
2020
```
2121
2222
## Return Value
@@ -44,73 +44,84 @@ For additional compatibility information, see [Compatibility](../../c-runtime-li
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 half-closed interval [range_min, range_max)
66+
// That is, range_min <= random number < range_max
67+
68+
for (int i = 0; i < n; i++)
69+
{
70+
// Note: This method of generating random numbers in a range is not suitable for
71+
// applications that require high quality randomn numbers.
72+
// rand() has a small output range [0,32767], making it unsuitable for
73+
// generating random numbers across a large range. This method also introduces bias.
74+
// More suitable random number functions are 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 ) );
81-
82-
SimpleRandDemo( 10 );
83-
printf("\n");
84-
RangedRandDemo( -100, 100, 10 );
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);
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, 10);
8591
}
8692
```
8793

8894
```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
95+
Simple random number demo ====
96+
97+
5890
98+
1279
99+
19497
100+
1207
101+
11420
102+
3377
103+
15317
104+
29489
105+
9716
106+
23323
107+
108+
Random number in a range demo ====
109+
110+
-82
111+
-46
112+
50
113+
77
114+
-47
115+
32
116+
76
117+
-13
118+
-58
119+
90
110120
```
111121

112122
## See also
113123

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

0 commit comments

Comments
 (0)