内部random()函数

编程入门 行业动态 更新时间:2024-10-28 11:31:28
本文介绍了内部random()函数 - 如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们通常使用random()函数在C ++中创建随机数。

We commonly use random() function to create random numbers in C++.

使用void srand(unsigned int seed)会改善结果,因为它会根据seed的值生成随机数。

The use of void srand (unsigned int seed) would improve the results since it would generate random numbers depending on the value of seed.

任何人都可以告诉我如何实现随机函数?

Can anyone tell me how the random function is actually implemented?

推荐答案

这里是对GNU C库的random()函数中使用的算法的详细描述。

Here is a detailed description of the algorithm used in the GNU C library's random() function.

基本上:

#include <stdio.h> #define MAX 1000 #define seed 1 main() { int r[MAX]; int i; r[0] = seed; for (i=1; i<31; i++) { r[i] = (16807LL * r[i-1]) % 2147483647; if (r[i] < 0) { r[i] += 2147483647; } } for (i=31; i<34; i++) { r[i] = r[i-31]; } for (i=34; i<344; i++) { r[i] = r[i-31] + r[i-3]; } for (i=344; i<MAX; i++) { r[i] = r[i-31] + r[i-3]; printf("%d\n", ((unsigned int)r[i]) >> 1); } }

更多推荐

内部random()函数

本文发布于:2023-10-09 09:29:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   random

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!