如何在C和C ++中使用rand()和srand()函数生成随机数?

C and C++ programming languages provide rand() and srand() functions in order to create random numbers. Random numbers can be used for security, lottery, etc. In this tutorial, we will learn how to use a random number generating functions rand() and srand() with their attributes and specialties.

C和C ++编程语言提供rand()srand()函数以创建随机数。 随机数可用于安全性,彩票等。在本教程中,我们将学习如何使用随机数生成函数rand()srand()及其属性和特殊性。

随机数 (Random Numbers)

Random numbers are sequential numbers that are different than each other always. Random numbers are important for security which can be used to generate UUID, Certificates, Passwords, etc. In order to generate random numbers we generally need a hardware-based source that generates random signals where these signals will be used to generate a random number. There are hardware components used to generate these random signals specifically.

随机数是始终彼此不同的序号。 随机数对于安全性很重要,可用于生成UUID,证书,密码等。为了生成随机数,我们通常需要基于硬件的源,该源会生成随机信号,其中将使用这些信号来生成随机数。 有专门用于生成这些随机信号的硬件组件。

PSEUDO随机数 (PSEUDO Random Numbers)

As using hardware-based random number generators are pricy or not always available we generally use random-like signal generators. If we are using this type of not completely random number generator we call these PSEUDO Random Number Generators. For example Network Interface Card, Sound Card or similar hardware can be used as PSEUDO Random Number Generator.

由于使用基于硬件的随机数生成器价格昂贵或并不总是可用,因此我们通常使用类似随机的信号生成器。 如果我们使用的是这种类型的不完全随机数生成器,我们将这些称为PSEUDO随机数生成器。 例如,网络接口卡,声卡或类似硬件可以用作PSEUDO随机数生成器。

种子价值 (Seed Value)

As creating randomness is a very hard job we can provide Seed for every random function execution to create randomness. Seed values are used to make a random start from the application point of view. Every time we need to use different seed values to make it perfect. We generally use functions like time, network traffic, etc where they are changing regularly.

由于创建随机性是一项非常艰巨的工作,我们可以为每个随机函数执行提供Seed来创建随机性。 从应用程序的角度来看,种子值用于随机启动。 每次我们需要使用不同的种子值来使其完美时。 我们通常使用时间,网络流量等功能,这些功能会定期更改。

RAND_MAX (RAND_MAX)

While generating random numbers we need to set some start and end limits. The start limit is 0 but the end limit can be defined explicitly with the RAND_MAX macro like below.

在生成随机数时,我们需要设置一些开始和结束限制。 起始限制为0,但可以使用RAND_MAX宏明确定义结束限制,如下所示。

#define RAND_MAX 100

rand()函数 (rand() Function)

rand() function is used to create random numbers without any parameters. This will create the same sequence in each run during the same execution.

rand()函数用于创建不带任何参数的随机数。 这将在同一执行期间的每次运行中创建相同的序列。

#include <stdio.h> 
#include <stdlib.h>

int main(void) 
{

for(int i = 0; i<10; i++) 
   printf(" %d ", rand()); 

printf("\n");
return 0; 
}
rand() Function
rand() Function
rand()函数

We can see that every time we call rand() function we get the same random numbers.

我们可以看到,每次调用rand()函数时,我们都会得到相同的随机数。

LEARN MORE  Linux Bash Default Environment Variables
了解更多Linux Bash默认环境变量

srand()函数(srand() Function)

srand() function really generates pseudo-random integers.  We will also provide a seed value in an unsigned integer. We will make seed value the return value of the time(0) function which will be different every execution of the application.

srand()函数实际上会生成伪随机整数。 我们还将提供一个无符号整数的种子值。 我们将种子值作为time(0)函数的返回值,该值将在应用程序的每次执行中都不同。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>

int main(void) 
{ 
   srand(time(0));

   for(int i = 0; i<10; i++) 
      printf(" %d ", rand()); 

   printf("\n");
   
   return 0; 
}
srand() Function
srand() Function
srand()函数

We can see that every time we call the random function we get a different value which is completely random.

我们可以看到,每次调用随机函数时,我们都会得到一个完全随机的不同值。

rand()函数与srand()函数 (rand() Function vs srand() Function)

rand() and srand() are very similar functions . rand() function actually calls the srand(1) which will generate always the same random number sequence.

rand()srand()是非常相似的函数。 rand()函数实际上调用srand(1) ,它将生成始终相同的随机数序列。

明确指定其他随机数范围 (Specify Different Random Number Range Explicitly)

We have learned that we can use RAND_MAX  to limit generated random numbers. But start number will be . We can specifically define a start and max number by using mod operations of the C and C++ programming languages.

我们了解到可以使用RAND_MAX限制生成的随机数。 但是开始号码是。 我们可以通过使用C和C ++编程语言的mod操作来具体定义起始数和最大数。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>

int main(void) 
{ 
   srand(time(0));

   for(int i = 0; i<10; i++) 
      printf(" %d ", (rand()%100)-20); 
   
   printf("\n");
   return 0; 
}
Specify Different Random Number Range Explicitly
Specify Different Random Number Range Explicitly
明确指定其他随机数范围

翻译自: https://www.poftut.com/how-to-generate-random-numbers-with-rand-and-srand-functions-in-c-and-cpp/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值