rand函数怎么用?

学术版

https://zh.cppreference.com/w/cpp/numeric/random/rand
by cjh20090318 @ 2024-03-28 20:56:52


[这里](https://blog.csdn.net/qq_43516928/article/details/118864806)
by songchengze @ 2024-03-28 20:57:27


[还有这里](https://zhuanlan.zhihu.com/p/390093811)
by songchengze @ 2024-03-28 20:59:14


`srand(time(0))`可以让你的程序每次运行调用`rand()`的值不同。 `rand()`可以认为是一个函数,返回随机值。 `a=rand()`。 如果想让 $a$ 取值在 $[l,r]$,如下: `a=rand()%(r-l+1)+l` 当然,`rand()`在 $32767$(左右?)范围内,所以可以考虑定义新`rand()`为`rand()*32768+rand()`这种形式来增加`rand()`范围,求关
by sbno333 @ 2024-03-28 21:00:05


@[Bill_luogu](/user/930506) 不建议用 `rand`,效率低且随机数质量差 建议用 `mt19937`,声明: ```cpp mt19937 rnd(time(0)); ``` 调用 `rnd()` 来获得在 $[0,2^{32})$ 均匀分布的整数。 `random` 库中还定义了 `uniform_int_distribution`,`uniform_real_distribution` 等很有用的东西,可以自行查阅 cppreference
by Untitled0 @ 2024-03-29 18:06:49


@[cjh20090318](/user/577880) @[songchengze](/user/922409) @[sbno333](/user/416975) @[Untitled0](/user/393767) 谢谢各位 $dalao$
by Bill_luogu @ 2024-03-29 21:35:34


|