rand.h
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef RAND_H unsigned long int next = 1; int rand() { next = next * 1103515245 + 12345; return (unsigned int)(next/65535) % 32768; } void seed(unsigned int seed) { next = seed; } #endif
main.c
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <stdio.h> #include "rand.h" int main(int argc, char **argv) { int num; while (1) { printf("Please enter the seedn"); scanf("%d", &num); seed(num); printf("The value is %dn", rand()); } return 0; }
使用%取模可以用于产生任意整形范围内的随机数。
最后
以上就是灵巧裙子最近收集整理的关于伪随机数的实现的全部内容,更多相关伪随机数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复