[英]Four digit random number without digit repetition
Is there any way you can have a 4 digit number without repetition - eg not 1130
but 1234
? 有什么办法可以不重复地使用4位数字-例如不是
1130
而是1234
? I read std::random_shuffle
could do this but it would only swap the numbers in between. 我读了
std::random_shuffle
可以做到这一点,但它只会在两者之间交换数字。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
unsigned seed = static_cast<size_t>(std::chrono::system_clock::now().time_since_epoch().count());
using namespace std;
class Player {
private:
string playername;
public:
void setName(string b) {
cout << "Please enter your name:" << endl;
getline(cin, b);
playername = b;
}
string getName () {
return playername;
}
};
class PasswordGuessingGame {
private:
std::mt19937 random_engine;
std::uniform_int_distribution<size_t> random_generator;
public:
PasswordGuessingGame():
random_engine(seed),
random_generator(1000,9999)
{
}
int getNumber () {
return random_generator(random_engine);
}
};
int main () {
Player newgame;
PasswordGuessingGame b;
newgame.setName("");
cout << newgame.getName() << " " << "password " << b.getNumber() << endl;
}
One possibility is to generate a string containing the digits, and to use the C++14 function std::experimental::sample()
一种可能是生成包含数字的字符串,并使用C ++ 14函数
std::experimental::sample()
#include <iostream>
#include <random>
#include <string>
#include <iterator>
#include <experimental/algorithm>
int main() {
std::string in = "0123456789", out;
do {
out="";
std::experimental::sample(in.begin(), in.end(), std::back_inserter(out), 4, std::mt19937{std::random_device{}()});
std::shuffle(out.begin(), out.end(), std::mt19937{std::random_device{}()});
} while (out[0]=='0');
std::cout << "random four-digit number with unique digits:" << out << '\n';
}
Edit: 编辑:
Changed to prevent a result that starts with a 0. Hat tip to @Bathsheba who indicated that this could be a problem. 进行了更改,以防止结果以0开头。@ Bathsheba的帽子提示指出这可能是个问题。
I think you need to generate each digit separately. 我认为您需要分别生成每个数字。 For example you have array from 0 to 9 with 0..9 digits.
例如,您有一个从0到9的数组,其中包含0..9位数。 For first digit you generate number from 0 to 9 and pick up digit from this array.
对于第一个数字,您会生成0到9之间的数字,并从此数组中提取数字。 Then you swap this array element t with last element of array.
然后将此数组元素t与数组的最后一个元素交换。 For second digit you generate number form 0 to 8. And so on.
对于第二个数字,您生成的数字格式为0到8。依此类推。
I don't see a problem with std::random_shuffle
: 我看不到
std::random_shuffle
有问题:
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
int main()
{
std::string s;
std::generate_n(std::back_inserter(s), 10,
[]() { static char c = '0'; return c++; });
// s is now "0123456789"
std::mt19937 gen(std::random_device{}());
// if 0 can't be the first digit
std::uniform_int_distribution<size_t> dist(1, 9);
std::swap(s[0], s[dist(gen)]);
// shuffle the remaining range
std::shuffle(s.begin() + 1, s.end(), gen); // non-deprecated version
// convert only first four
auto x = std::stoul(s.substr(0, 4));
std::cout << x << std::endl;
}
If you can count how many valid (ie acceptable) sequences exist, and you can devise a bijective function that maps from this counter to each valid sequence instance then things become trivial. 如果您可以计算存在多少有效(即可接受)序列,并且可以设计出一个双射函数,将这个计数器映射到每个有效序列实例,那么事情就变得微不足道了。
If I understand your example correctly, you want to generate a random 4 digit sequence ABCD (representing an integer in the range [0,9999]) where digits A , B , C and D are different from one another. 如果我正确理解了您的示例,则希望生成一个随机的4位数序列ABCD(表示[0,9999]范围内的整数),其中A , B , C和D的数字彼此不同。
There are 5040 such valid sequences: 10 * 9 * 8 * 7. 有5040个这样的有效序列:10 * 9 * 8 * 7。
Given any integer in the range [0, 5039], the following function will return a valid sequence (ie one in which each digit is unique), represented as an integer: 给定[0,5039]范围内的任何整数,以下函数将返回一个有效的序列(即每个数字都是唯一的序列),以整数表示:
int counter2sequence(int u) {
int m = u/504;
u %= 504;
int h = u/56;
u %= 56;
int t = u/7;
u %= 7;
const int ih = h;
const int it = t;
if (ih >= m) ++h;
if (it >= ih) ++t;
if (t >= m) ++t;
if (u >= it) ++u;
if (u >= ih) ++u;
if (u >= m) ++u;
return ((m*10 + h)*10 + t)*10 + u;
}
Eg 例如
counter2sequence(0) => 0123
counter2sequence(5039) => 9876
Another method, using only standard data and numeric algorithms. 另一种方法,仅使用标准数据和数字算法。
#include <random>
#include <array>
#include <iostream>
#include <numeric>
template<class Engine>
int unrepeated_digits(int ndigits, Engine &eng) {
std::array<int, 10> digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto add_digit = [](auto x, auto digit) {
return x * 10 + digit;
};
std::shuffle(std::begin(digits), std::end(digits), eng);
return std::accumulate(std::begin(digits), std::next(std::begin(digits), ndigits), 0, add_digit);
}
int main() {
std::random_device rnd;
std::default_random_engine eng(rnd());
for (int i = 0; i < 10; ++i)
std::cout << unrepeated_digits(4, eng) << std::endl;
}
example output: 示例输出:
7623
3860
9563
9150
3219
8652
4789
2457
1826
9745
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.