首页 > 编程知识 正文

srand和rand函数了解C rand和srand函数,c中srand和rand函数

时间:2023-05-04 00:33:02 阅读:222725 作者:4791

srand和rand函数

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

嘿crdfk! 在本文中,我们将重点介绍使用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:

输出:

180428938384693088616816927771714636915195774779342423833571988538616497604925965166491189641421

什么是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:

输出:

93126597914796869461788426935135914512358201107237501535463112864611813442261858016203138472912

对生成的随机数执行数学运算 (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:

输出:

84795079847481567716

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) C++ rand() function — Documentation

C ++ rand()函数—文档 C++ srand() function — Documentation

C ++ srand()函数—文档

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

srand和rand函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。