#include <iostream>
#include <ctime>
using namespace std;
int randBetween()
{
unsigned seed = time(0);
srand(seed);
const int MIN_VALUE = -100;
const int MAX_VALUE = 100;
return (rand() % (MAX_VALUE - MIN_VALUE 1 )) MIN_VALUE;
}
int main() {
const int SIZE = 10;
int myArray[SIZE];
// ^^ how do I use function above to give myArray random values?
return 0;
}
我想使用該 rand 函式為我的陣列提供從 -100 到 100 的隨機值,但我不知道如何將該 rand 函式放入陣列中,以便我的陣列可以在其中生成亂數,希望這有意義我該怎么做?
uj5u.com熱心網友回復:
首先,我們將查看您的代碼并對其進行評論。
#include <iostream>
#include <ctime>
// MISSING <cstdlib> for C random functions
using namespace std; // Bad Practice
int randBetween()
{
unsigned seed = time(0); // Wrong placement; should only instantiate ONCE
srand(seed);
const int MIN_VALUE = -100;
const int MAX_VALUE = 100;
return (rand() % (MAX_VALUE - MIN_VALUE 1 )) MIN_VALUE;
// Modulo math tends to make the values in the lower end of the range more prevalent; i.e.,
// it's not very uniform.
}
int main() {
const int SIZE = 10; // All caps names for constants is not desirable; they can be confused for macros
int myArray[SIZE]; // Prefer std::array if the size is known, else std::vector for most cases
// ^^ how do I use function above to give myArray random values?
return 0;
}
當 C 提供更好的方法時,最大的問題是使用 C 風格的約定。事實上,您甚至不需要為此提供一個函式。
將亂數放入陣列的秘訣是回圈。讓您的回圈訪問每個元素并分配一個新的亂數。要么直接,如我的第一個示例,要么使用我的第二個示例中的函式。
#include <array>
#include <iostream>
#include <random>
int main() {
const int minValue = -100;
const int maxValue = 100;
const int size = 10;
std::array<int, size> myArray;
// std::mt19937 is the goto PRNG in <random>
// This declaration also seeds the PRNG using std::random_device
// A std::uniform_int_distribution is exactly what it sounds like
// Every number in the range is equally likely to occur.
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> dist(minValue, maxValue);
for (auto& i : myArray) {
i = dist(prng);
}
for (auto i : myArray) {
std::cout << i << ' ';
}
std::cout << '\n';
}
現在,如果您想要或需要該功能,則需要做一些額外的作業。
#include <array>
#include <iostream>
#include <random>
int randBetween() {
const int minValue = -100;
const int maxValue = 100;
// The keyword static is required now so that the PRNG and distribution
// are not re-instantiated every time the function is called. This is
// important for them both to work as intended. Re-instantiating resets
// their state, and they constantly start from scratch. They must be allowed
// to persist their state for better results.
static std::mt19937 prng(std::random_device{}()); // Note the static
static std::uniform_int_distribution<int> dist(minValue, maxValue);
return dist(prng);
}
int main() {
const int size = 10;
std::array<int, size> myArray;
for (auto& i : myArray) {
i = randBetween();
}
for (auto i : myArray) {
std::cout << i << ' ';
}
std::cout << '\n';
}
將 PRNG 與分發分開是一種很好的做法,尤其是當程式變大時。然后,如果需要,您的單個 PRNG 可以提供多個發行版。
我得到的一個輸出:
-2 -37 81 85 -38 -62 31 -15 -12 -31
uj5u.com熱心網友回復:
將評論匯總為答案:
你可以使用一個回圈:
for (size_t i = 0; i < SIZE; i)
{
myArray[i] = randBetween();
}
或者您可以使用標準演算法std::generate:
std::generate(std::begin(myArray), std::end(myArray), randBetween);
這將遍歷陣列,呼叫randBetween每個成員并將其分配給結果。
但是,您有一個大問題。你不應該srand每次想要一個亂數時都打電話。呼叫它一次,在main. 正如您現在擁有的那樣,您可能會為給定的運行用一個數字填充陣列。如果在程式運行期間當前時間的秒數發生變化,則數字在整個陣列中發生變化的可能性很小。
此外,用于亂數的C 標準庫比rand. 我建議使用標準容器而不是本機陣列。
uj5u.com熱心網友回復:
另一種方法是使用 PRNG(偽亂數生成器)。這是來自包含隨機,包含功能標頭。像這樣,在 C 17 編譯器中運行它:
#include <iostream>
#include <random>
#include <functional> // For std::bind()
auto createUniformPseudoRandomNumberGenerator(double max)
{
std::random_device seeder;
std::default_random_engine generator{seeder()};
std::uniform_real_distribution distribution{0.0, max};
return std::bind(distribution, generator);
}
int main()
{
const int SIZE = 10;
int myArray[SIZE];
double limit{};
std::cout<<"Enter max number limit.\n";
std::cin>>limit;
static auto random_number=createUniformPseudoRandomNumberGenerator(limit);
for (size_t i{};i<SIZE; i)
{
myArray[i]= random_number();
}
return 0;
}
鏈接:https : //onlinegdb.com/686M8KB1p
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376154.html
上一篇:在R函式中輸入多個變數
