Random numbers just numbers that lie within a range and any of the numbers can occur.
随机数只是在一个范围内的数字,任何数字都可能出现。
In programming, we come through a lot of scenarios where we need to generate random numbers. Like for dice games, lottery system, etc. In the c programming language, there is an inbuilt method to take care of this.
在编程中,我们遇到许多需要生成随机数的场景。 就像骰子游戏,彩票系统等一样。在c编程语言中,有内置的方法来处理此问题。
srand() and rand() functions are used to generate random numbers. Now, let's dig deep into these methods and understand their working.
srand()和rand()函数用于生成随机数 。 现在,让我们深入研究这些方法并了解它们的工作原理。
rand()函数 (rand() function)
The rand() function in the C programming language is used to generate a random number. The return type is of rand() function is an integer.
C编程语言中的rand()函数用于生成随机数。 返回类型为rand(),函数为整数。
C code to generate a random number
C代码生成一个随机数
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Random number is: %d ", rand());
return 0;
}
Output
输出量
Random number is: 1804289383
srand()函数 (srand() function)
The srand() function is used to set the starting value for the series of random integers. You can use the srand() to set the seed of the rand function to a different starting point.
srand()函数用于设置一系列随机整数的起始值。 您可以使用srand()将rand函数的种子设置为其他起点。
The parameters that are passed into the srand() function are the starting values for the rand method.
传递给srand()函数的参数是rand方法的起始值。
C code to generate random numbers using srand()
C代码使用srand()生成随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(0));
printf("%d ", rand());
return 0;
}
Output
输出量
1370632410
生成一定范围内的随机数 (Generating random numbers within a range )
The rand() function generates random numbers that can be any integer value. But, to generate random numbers within a specific range, we have a formula that returns a random number between given ranges.
rand()函数生成可以是任何整数值的随机数。 但是,要生成特定范围内的随机数 ,我们有一个公式可以返回给定范围之间的随机数。
Formula:
式:
number = (rand() % (upper - lower + 1)) + lower
Program to generate random numbers from 1 to 6
程序生成1到6的随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int lower = 1, upper = 6, count = 10;
srand(time(0));
printf("The random numbers are: ");
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d ", num);
}
return 0;
}
Output
输出量
The random numbers are: 1 5 4 4 4 2 3 2 4 3
Explanation:
说明:
This program declares a variable num that generates a random number between 1 to 6. for doing this we will initialize the lower limit of the rand to 1 and The upper limit of the rand function to 6. this will generate a random number between 1 to 6 using the formula.
该程序声明一个变量num,该变量生成一个1到6之间的随机数。为此,我们将rand的下限初始化为1并将rand函数的上限初始化为6。这将生成一个1到6之间的随机数。 6使用公式。
Program to generate random number between 1 to 100
程序生成1到100之间的随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int lower = 1, upper = 100, count = 10;
srand(time(0));
printf("The random numbers are: ");
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d ", num);
}
return 0;
}
Output
输出量
The random numbers are: 50 22 52 69 24 98 39 31 77 97
翻译自: https://www.includehelp.com/c-programs/generate-random-numbers-within-a-range.aspx