随机数产生的三种算法
2008-07-15 16:08
246 查看
/*从同一个种子开始*/
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;
int rand0(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
int main(void)
{
int count;
for(count=0;count<5;count++)
printf("%hd/n",rand0());
getch();
return 0;
}
/*重置种子*/
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;
int rand1(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
void srand1(unsigned int seed)
{
next=seed;
}
int main(void)
{
int count;
unsigned int seed;
printf("please input seed:");
scanf("%u",&seed);
srand1(seed);
for(count=0;count<5;count++)
printf("%hd/n",rand1());
getch();
return 0;
}
/* 利用利用时钟产生种子
ANSI C程序库提供了rand()函数来产生随机数;
ANSI C程序库提供了srand()函数来产生种子;
ANSI C程序库提供了time()函数返回系统时间。
*/
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
int i;
time_t t;
clrscr();
t = time(NULL);
srand((unsigned) t);
for(i=0; i<10; i++) printf("%d/n", rand()%10);
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;
int rand0(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
int main(void)
{
int count;
for(count=0;count<5;count++)
printf("%hd/n",rand0());
getch();
return 0;
}
/*重置种子*/
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;
int rand1(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
void srand1(unsigned int seed)
{
next=seed;
}
int main(void)
{
int count;
unsigned int seed;
printf("please input seed:");
scanf("%u",&seed);
srand1(seed);
for(count=0;count<5;count++)
printf("%hd/n",rand1());
getch();
return 0;
}
/* 利用利用时钟产生种子
ANSI C程序库提供了rand()函数来产生随机数;
ANSI C程序库提供了srand()函数来产生种子;
ANSI C程序库提供了time()函数返回系统时间。
*/
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
int i;
time_t t;
clrscr();
t = time(NULL);
srand((unsigned) t);
for(i=0; i<10; i++) printf("%d/n", rand()%10);
getch();
return 0;
}
相关文章推荐
- 算法练习:产生指定范围的随机数
- 产生不等随机数算法
- 产生随机数的三种方式
- java基础算法之随机数的产生
- C语言之实现随机数产生算法
- 学习笔记之java中三种随机数的产生方法
- 典型算法及应用——关于产生不重复随机数的算法
- 设计一个算法,将计算机产生的n个随机数,分为奇数、偶数两组,并将它们分别压入两个栈中,然后输出在屏幕上
- JavaScript 产生不重复的随机数三种实现思路
- Java数组排序基础算法,二维数组,排序时间计算,随机数产生
- 编程珠玑里随机数产生算法
- 算法竞赛中的随机数产生和断言
- Java数组排序基础算法,二维数组,排序时间计算,随机数产生
- objective-c 中三种产生随机数的方法
- C语言之实现随机数产生算法
- 关于产生不重复随机数的算法
- 算法:如何高效产生m个n范围内的不重复随机数(m<=n)
- 算法--生成m个指定范围的不重复随机数的三种方法分析(Java实现)