You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
15
15
16
16
## Syntax
17
17
18
18
```C
19
-
intrand(void);
19
+
intrand(void);
20
20
```
21
21
22
22
## Return Value
@@ -27,7 +27,7 @@ int rand( void );
27
27
28
28
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`**.
29
29
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).
31
31
32
32
By default, this function's global state is scoped to the application. To change this, see [Global state in the CRT](../global-state.md).
33
33
@@ -37,80 +37,90 @@ By default, this function's global state is scoped to the application. To change
37
37
|-------------|---------------------|
38
38
|**`rand`**|`<stdlib.h>`|
39
39
40
-
For additional compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
40
+
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
41
41
42
42
## Example
43
43
44
44
```C
45
45
// crt_rand.c
46
46
// 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.
49
50
50
-
#include <stdlib.h>
51
-
#include <stdio.h>
52
-
#include <time.h>
51
+
#include <stdlib.h> // rand(), srand()
52
+
#include <stdio.h> // printf()
53
53
54
-
void SimpleRandDemo(int n)
54
+
void SimpleRandDemo(int n)
55
55
{
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
+
}
60
61
}
61
62
62
-
void RangedRandDemo(int range_min, int range_max, int n)
63
+
void RangedRandDemo(int range_min, int range_max, int n)
63
64
{
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
+
}
74
79
}
75
80
76
-
int main(void)
81
+
int main(void)
77
82
{
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);
81
86
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");
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2259.md
+7-13
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
---
2
2
description: "Learn more about: Compiler Error C2259"
3
3
title: "Compiler Error C2259"
4
-
ms.date: "11/04/2016"
4
+
ms.date: 07/08/2021
5
5
f1_keywords: ["C2259"]
6
6
helpviewer_keywords: ["C2259"]
7
7
ms.assetid: e458236f-bdea-4786-9aa6-a98d8bffa5f4
8
8
---
9
9
# Compiler Error C2259
10
10
11
-
'class' : cannot instantiate abstract class
11
+
> '*class*' : cannot instantiate abstract class
12
12
13
13
Code declares an instance of an abstract class or structure.
14
14
15
-
You cannot instantiate a class or structure with one or more pure virtual functions. To instantiate objects of a derived class, the derived class must override each pure virtual function.
15
+
You can't instantiate a class or structure with one or more pure virtual functions. To instantiate objects of a derived class, the derived class must override each pure virtual function.
16
16
17
17
For more information, see [Implicitly abstract classes](../../dotnet/how-to-define-and-consume-classes-and-structs-cpp-cli.md#BKMK_Implicitly_abstract_classes).
18
18
@@ -35,15 +35,11 @@ A a; // C2259, A inherits func() as pure virtual
35
35
B b; // OK, B defines func()
36
36
```
37
37
38
-
Whenever you derive from an interface and implement the interface methods in the derived class with access permissions other than public, you may receive C2259. This occurs because the compiler expects the interface methods implemented in the derived class to have public access. When you implement the member functions for an interface with more restrictive access permissions, the compiler does not consider them to be implementations for the interface methods defined in the interface, which in turn makes the derived class an abstract class.
38
+
Whenever you derive from an interface and implement the interface methods in the derived class with access permissions other than `public`, you may receive C2259. The error occurs because the compiler expects the interface methods implemented in the derived class to have `public` access.
39
39
40
-
There are two possible workarounds for the problem:
40
+
To resolve this issue, don't use more restrictive access permissions for the implementation methods. The compiler doesn't consider them as implementations for the interface methods defined in the interface. In turn, that makes the derived class an abstract class. Instead, make the access permissions `public` for the implemented methods.
41
41
42
-
- Make the access permissions public for the implemented methods.
43
-
44
-
- Use the scope resolution operator for the interface methods implemented in the derived class to qualify the implemented method name with the name of the interface.
45
-
46
-
C2259 can also occur as a result of conformance work that was done in Visual Studio 2005, **/Zc:wchar_t** is now on by default. In this situation, C2599 can be resolved either by compiling with **/Zc:wchar_t-**, to get the behavior from previous versions, or preferably, by updating your types so they are compatible. For more information, see [/Zc:wchar_t (wchar_t Is Native Type)](../../build/reference/zc-wchar-t-wchar-t-is-native-type.md).
42
+
C2259 can also occur because of conformance work that was done in Visual Studio 2005, **`/Zc:wchar_t`** is now on by default. In this situation, C2599 can be resolved either by compiling with **`/Zc:wchar_t-`**, to get the behavior from previous versions, or preferably, by updating your types so they're compatible. For more information, see [`/Zc:wchar_t` (wchar_t Is Native Type)](../../build/reference/zc-wchar-t-wchar-t-is-native-type.md).
47
43
48
44
The following sample generates C2259:
49
45
@@ -96,9 +92,7 @@ ref class MyDerivedClass: public MyInterface {
0 commit comments