Skip to content

Repo sync for protected CLA branch #3244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 9, 2021
2 changes: 1 addition & 1 deletion .whatsnew.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"relativeLinkPrefix": "../"
},
"inclusionCriteria": {
"minAdditionsToFile": 10,
"minAdditionsToFile": 3,
"pullRequestTitlesToIgnore": [
"^Confirm merge from FromPublicMasterBranch",
"^Repo sync for protected CLA branch"
Expand Down
130 changes: 70 additions & 60 deletions docs/c-runtime-library/reference/rand.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "rand"
description: "API reference for rand, which generates a pseudorandom number by using a well-known and fully-reproducible algorithm."
ms.date: "4/2/2020"
description: "API reference for rand, which generates a pseudorandom number by using a well-known and fully reproducible algorithm."
ms.date: "7/7/2021"
api_name: ["rand", "_o_rand"]
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"]
api_type: ["DLLExport"]
Expand All @@ -11,12 +11,12 @@ helpviewer_keywords: ["generating pseudorandom numbers", "random numbers, genera
---
# `rand`

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).
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).

## Syntax

```C
int rand( void );
int rand(void);
```

## Return Value
Expand All @@ -27,7 +27,7 @@ int rand( void );

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`**.

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).
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).

By default, this function's global state is scoped to the application. To change this, see [Global state in the CRT](../global-state.md).

Expand All @@ -37,80 +37,90 @@ By default, this function's global state is scoped to the application. To change
|-------------|---------------------|
|**`rand`**|`<stdlib.h>`|

For additional compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).

## Example

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

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

void SimpleRandDemo( int n )
void SimpleRandDemo(int n)
{
// Print n random numbers.
int i;
for( i = 0; i < n; i++ )
printf( " %6d\n", rand() );
// 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 )
void RangedRandDemo(int range_min, int range_max, int n)
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
for ( i = 0; i < n; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;
printf( " %6d\n", u);
}
// 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://docs.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 )
int main(void)
{
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );
// Seed the random-number generator with a fixed seed so that
// the numbers will be the same every time we run.
srand(1792);

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

```Output
22036
18330
11651
27464
18093
3284
11785
14686
11447
11285

74
48
27
65
96
64
-5
-42
-55
66
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

[Floating-Point Support](../../c-runtime-library/floating-point-support.md)<br/>
[`srand`](srand.md)<br/>
[`rand_s`](rand-s.md)<br/>
[Floating-Point Support](../../c-runtime-library/floating-point-support.md)\
[`srand`](srand.md)\
[`rand_s`](rand-s.md)\
[C++ `<random>` library](../../standard-library/random.md)
20 changes: 7 additions & 13 deletions docs/error-messages/compiler-errors-1/compiler-error-c2259.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
description: "Learn more about: Compiler Error C2259"
title: "Compiler Error C2259"
ms.date: "11/04/2016"
ms.date: 07/08/2021
f1_keywords: ["C2259"]
helpviewer_keywords: ["C2259"]
ms.assetid: e458236f-bdea-4786-9aa6-a98d8bffa5f4
---
# Compiler Error C2259

'class' : cannot instantiate abstract class
> '*class*' : cannot instantiate abstract class

Code declares an instance of an abstract class or structure.

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.
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.

For more information, see [Implicitly abstract classes](../../dotnet/how-to-define-and-consume-classes-and-structs-cpp-cli.md#BKMK_Implicitly_abstract_classes).

Expand All @@ -35,15 +35,11 @@ A a; // C2259, A inherits func() as pure virtual
B b; // OK, B defines func()
```

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.
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.

There are two possible workarounds for the problem:
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.

- Make the access permissions public for the implemented methods.

- 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.

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).
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).

The following sample generates C2259:

Expand Down Expand Up @@ -96,9 +92,7 @@ ref class MyDerivedClass: public MyInterface {
private:
// Uncomment the following line to resolve.
// public:
void MyMethod(){}
// or the following line
// void MyInterface::MyMethod() {};
virtual void MyMethod(){}
};

int main() {
Expand Down
Loading