我怎樣才能有效地填充包含隨機 u-short 整數的元素向量,這些整數之間必須完全不同?
(我已經知道如何使用亂數填充向量:
#include <iostream>
#include <random>
int main() {
//INPUTs
unsigned short int n_of_all_students, n_called_students;
std::cout << "Enter how many students do you have: _";//(for example)
std::cin >> n_of_all_students;
std::cout << "Enter how many students you'd like to call: _";
std::cin >> n_called_students;
//SEED, ENGINE AND RANGE DEFINITIONs
std::random_device random_device; // creates the seed
std::mt19937_64 mt64{ random_device() }; //creates the engine (Mersenne Twister, 64 bit) and assignes to it the just created seed
std::uniform_int_distribution<> al_range(1, n_of_all_students);//the range which the numbers will be created into
//VECTOR FILLING
std::vector<short int> called_students;
for (auto i = 0; i < n_called_students; i) {
called_students.push_back(al_range(mt64));// puts, finally, the just generated random number into the vector
//OUTPUT
std::cout << i 1 << ":\t" << called_students[i] << '\n';
}
}
,但我不知道如何只用不同的數字填充它
我也不知道是否,
在那之后,
將每個數字相互比較會更有效,不斷用其他亂數替換相等的數字,直到它們都不同
或者,
從一個空向量開始,直接用隨機的不同數字填充它)
哪一個是最有效的解決方案?
我怎么寫?
uj5u.com熱心網友回復:
對于相當小的有限值域,一種流行的方法是簡單地生成可能值的床,然后對其進行洗牌,然后提取您需要的內容。
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <random>
int main()
{
unsigned short int n_of_all_students, n_called_students;
std::cout << "Enter how many students do you have: ";
std::cin >> n_of_all_students;
std::cout << "Enter how many students you'd like to call: ";
std::cin >> n_called_students;
// build ids.
std::vector<unsigned short int> ids(n_of_all_students);
std::iota(ids.begin(), ids.end(), 1);
// shuffle the ids
std::mt19937_64 mt64{ std::random_device{}() };
std::shuffle(ids.begin(), ids.end(), mt64);
// pull the called count.
std::vector<unsigned short int> called_students(
ids.begin(), std::next(ids.begin(), n_called_students) );
std::sort(called_students.begin(), called_students.end());
// print results.
for (auto x : called_students)
std::cout << x << ' ';
std::cout << '\n';
}
輸入
Enter how many students do you have: 40
Enter how many students you'd like to call: 20
輸出(顯然不同)
1 2 4 5 9 12 13 16 22 23 24 26 27 28 29 31 34 35 37 39
另一個具有相同輸入條件的樣本:
2 3 8 9 12 13 14 17 19 21 22 23 26 29 31 32 35 36 38 40
注意:這sort是可選的,但可以很好地展示。
這種方法也被稱為“從帽子里拉出來”,因為同樣的方法適用于“隨機”繪圖。把所有的名字都放在帽子里,搖晃帽子,然后抽出沒有替換的數字。
值得一提的是,上面的代碼沒有內置的悲劇避免有人要求從大小為 N 的池中提取 > N 個 id。顯然這是不可能的,并且嘗試這樣做會呼叫未定義的行為代碼沒有正確檢查n_called_students小于或等于n_of_all_students,并且兩者都是正數。我留給你的清理作業。
uj5u.com熱心網友回復:
正如其他評論指出的那樣,最有效的方法是用您要填充的數字填充向量,然后隨機播放。您將需要<algorithm>標頭,并std::shuffle()執行此操作:
#include <iostream>
#include <random>
#include <algorithm>
int main() {
//INPUTs
unsigned short int n_of_all_students, n_called_students;
std::cout << "Enter how many students do you have: _";//(for example)
std::cin >> n_of_all_students;
std::cout << "Enter how many students you'd like to call: _";
std::cin >> n_called_students;
//SEED, ENGINE AND RANGE DEFINITIONs
std::random_device random_device; // creates the seed
std::mt19937_64 mt64{ random_device() }; //creates the engine (Mersenne Twister, 64 bit) and assignes to it the just created seed
std::uniform_int_distribution<> al_range(1, n_of_all_students);//the range which the numbers will be created into
//VECTOR FILLING
std::vector<short int> called_students;
for (auto i = 1; i <= n_of_all_students; i) {
called_students.push_back(i);// puts, finally, the just generated random number into the vector
}
std::shuffle(called_students.begin(), called_students.end(), mt64);
//OUTPUT
for(auto i=0; i<n_called_students; i)
std::cout << i 1 << ":\t" << called_students[i] << '\n';
}
uj5u.com熱心網友回復:
看來你想要std::sample(C 17):
// build ids.
std::vector<unsigned short int> ids(n_of_all_students);
std::iota(ids.begin(), ids.end(), 1);
// sample the ids
std::mt19937_64 mt64{ std::random_device{}() };
std::vector<unsigned short int> called_students;
std::sample(ids.begin(), ids.end(),
std::back_inserter(called_students),
n_called_students,
mt64);
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/425692.html
標籤:C
上一篇:>"Symfony\Component\DependencyInjection\ContainerInterface"自動裝配>別名已棄用。如何在symfony5
