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