C语言写随机数

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static unsigned long int next = 1;
unsigned int rand0();
void srand1(unsigned int seed);
int main()
{
    int count;
    unsigned int seed;
    while(scanf("%u",&seed)==1)
    {
        srand1(seed);
        //可用变化的时间来做种子
        //srand1((unsigned int)time(0));
        for(count=0;count<5;count++)
        {
            printf("%d\n",rand0());
        }
    }
    return 0;
}
unsigned int rand0()
{
    next = next * 1103515245 + 12345;
    return (unsigned int)(next / 65536)%32768;
}
void srand1(unsigned int seed)
{
    next = seed;
}

 

posted @ 2017-08-31 09:34  疯陈演义  阅读(219)  评论(0编辑  收藏  举报