srand和rand函数_了解C ++ rand()和srand()函数

srand和rand函数

Hey, folks! In this article, we will be focusing on the generation of random numbers using C++ rand() and srand() function.

嘿伙计! 在本文中,我们将重点介绍使用C ++ rand()和srand()函数生成随机数。



什么是C ++ rand()函数? (What is C++ rand() function?)

The C++ rand() function is used to generate random numbers at compile time. As soon as the system encounters the rand() function, it generates a random set of numbers as output.

C++ rand() function用于在编译时生成随机数。 系统一遇到rand()函数,就会生成一组随机数字作为输出。

Syntax:

句法:


rand()

Using rand() function, when we compile and run the program, the system generates the same set of random numbers.

使用rand()函数,当我们编译并运行程序时,系统会生成相同的随机数集。



使用rand()生成您的第一个随机数 (Generating Your First Random Number using rand())


#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
    int random_num = rand();
    cout<<random_num;
}

Every time you try to run the above code, it will yield the same output random number as below.

每次您尝试运行上述代码时,它都会产生与以下相同的输出随机数。

Output:

输出:


1804289383


使用rand()生成多个随机数 (Generating Multiple Random Numbers using rand())


#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{ 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()<<endl;
    return 0; 
}

In this example, we have generated 10 random numbers using a for loop along with rand() function, respectively.

在此示例中,我们分别使用for循环和rand()函数生成了10个随机数。

Output:

输出:


1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421


什么是C ++ srand()函数? (What is C++ srand() function?)

C++ srand() is considered as Set seed for rand() function.

C ++ srand()被视为rand()函数的Set种子。

The srand() function along with rand() function is used to generate random numbers at compile time.

srand() function与rand()函数一起用于在编译时生成随机数。

C++ srand() function sets the beginning/starting point for the execution of random numbers by the rand() function. Usually, the srand() function is set and called once before the execution of rand() function in the program.

C++ srand() function设置rand()函数执行随机数的起点/起点 。 通常,在程序中执行rand()函数之前,将对srand()函数进行设置和调用一次。

Syntax:

句法:


srand(time())

The time() function returns the current time of the system. So, every time we execute the code, the value passed to the seed function would change according to the time. Thus, a new random set of numbers get generated whenever we execute the program, unlike rand() function.

time() function返回系统的当前时间。 因此,每次执行代码时,传递给种子函数的值都会根据时间而变化。 因此,无论何时执行程序,都会生成新的随机数字集,这与rand()函数不同。

The standard value to the srand() function is time(0).

srand()函数的标准值为time(0)。



以时间为种子的随机数 (Random Numbers With Time as the Seed)


#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
   srand(time(0)); 
   int rand_num = rand();
   cout<<rand_num;
}

Every time we execute the above code, a new random number is generated.

每当我们执行上述代码时,都会生成一个新的随机数。

Output:

输出:


153261608


使用srand()生成多个随机数 (Generating Multiple Random Numbers Using srand())


#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{  
   srand(time(0)); 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()<<endl;
    return 0; 
}

Output:

输出:


931265979
1479686946
1788426935
1359145123
582011072
375015354
631128646
1181344226
1858016203
138472912


对生成的随机数执行数学运算 (Performing Mathematical Operations on the Generated Random Number)


#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{  
   srand(time(0)); 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()%100<<endl;
    return 0; 
}

Output:

输出:


84
79
50
79
84
74
81
56
77
16


C ++ rand()和srand()函数一目了然! (C++ rand() and srand() function at a glance!)

  • The rand() function generates numbers randomly.

    rand()函数随机生成数字。
  • When execute the rand() function in a program, the same random number gets represented.

    在程序中执行rand()函数时,将表示相同的随机数。
  • The srand() function along with the rand() function generates random numbers at compile time.

    srand()函数与rand()函数一起在编译时生成随机数。
  • The srand() function does not return any value while the rand() function returns the random number generated by it.

    srand()函数不返回任何值,而rand()函数返回由它生成的随机数。


结论 (Conclusion)

Thus, in this article we have understood the working of C++ rand() function and srand() function respectively.

因此,在本文中,我们分别了解了C ++ rand()函数和srand()函数的工作。



参考资料 (References)

翻译自: https://www.journaldev.com/40344/rand-srand-c-plus-plus

srand和rand函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值