System::Random クラス
コンテンツ
[
隠れる
]Random class
疑似乱数生成器を表します。このクラスのオブジェクトは System::MakeObject() 関数を使用してのみ割り当てるべきです。スタック上や operator new を使用してこの型のインスタンスを作成しないでください。そうしないと実行時エラーやアサーション失敗が発生します。常にこのクラスを System::SmartPtr ポインタでラップし、そのポインタを関数への引数として使用してください。
class Random : public System::Object
メソッド
| メソッド | 説明 |
|---|---|
| IsNull() const | 常に false を返します。 |
| virtual Next() | int32 の最大値未満の非負の乱数を返します。 |
| virtual Next(int32_t) | 指定された最大値未満の非負の乱数を返します。 |
| virtual Next(int32_t, int32_t) | 指定された範囲内の乱数を返します。 |
| virtual NextBytes(const ArrayPtr<uint8_t>&) | 指定されたバイト配列の要素を乱数で埋めます。 |
| virtual NextDouble() | 0.0 から 1.0 の間の乱数を返します。 |
| Random() | 時間依存のデフォルトシード値を使用して新しいインスタンスを初期化します。 |
| Random(int32_t) | 指定されたシード値を使用して System.Random クラスの新しいインスタンスを初期化します。 |
備考
#include "system/random.h"
#include "system/smart_ptr.h"
#include <iostream>
int main()
{
const auto rnd = System::MakeObject<System::Random>();
// ランダムな月番号を取得し、出力します。
auto monthNumber = rnd->Next(1, 13);
std::cout << "Month: " << monthNumber << std::endl;
// 配列を乱数で埋めます。
auto arr = System::MakeObject<System::Array<uint8_t>>(12);
rnd->NextBytes(arr);
// 配列を印刷します。
for (auto i = 0; i < arr->get_Length(); ++i)
{
std::cout << static_cast<int>(arr[i]) << ' ';
}
std::cout << std::endl;
return 0;
}
/*
This code example produces the following output:
Month: 4
177 213 89 240 68 182 18 96 109 131 1 78
*/
参照
- Class Object
- Namespace System
- Library Aspose.Page for C++