简体   繁体   English

需要生成一个随机数,但该数字中的每个数字都不能高于 6(在 C 中)

[英]need to generate a random number, but every digit in that number can't be higher than 6 (in C)

I need to generate a random number that has 4 digits, but I have to make sure that every digit is not higher than 6, Is it possible?我需要生成一个有 4 位的随机数,但我必须确保每个数字不高于 6,可以吗? if so, how can I do that?如果是这样,我该怎么做? :) (In C) :) (在 C 中)

Simplest option would be to generate 4 different random numbers in the range of 0 to 6, then add them up, but multiply the first one by 1, second one by 10 etc.最简单的选择是生成 0 到 6 范围内的 4 个不同的随机数,然后将它们相加,但将第一个乘以 1,第二个乘以 10,依此类推。

int a = rand() % 7;
int b = rand() % 7;
int c = rand() % 7;
int d = rand() % 7;

int e = a + 10 * b + 100 * c + 1000 * d;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM