可能重复:
Generate Random numbers uniformly over entire range
How to use rand function to如何使用rand()生成范围内的数字?
谁能告诉我怎么用C编程的rand()函数,其中1 =分钟和
7 = MAX,1包括7个。
感谢
可能重复:
Generate Random numbers uniformly over entire range
How to use rand function to如何使用rand()生成范围内的数字?
谁能告诉我怎么用C编程的rand()函数,其中1 =分钟和
7 = MAX,1包括7个。
感谢
这会做你想要什么:
rand() % 7 + 1
说明:
rand()
返回0
和一大批之间的随机数。
% 7
得到除以7
后的余数,其将为从0
到6
(含)的整数。
+ 1
将范围更改为1
至7
(含)。
使用模运算符。它在分割时返回余数。通过使用 rand() % 7
生成范围为0到6的数字。加1即可生成范围为1至7的数字。
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ – onit
http://www.amazon.com/Programming-Language-第二,布莱恩 - 凯尼汉/ DP/0131103628 –