c语言随机数的问题,从速~~~~在线等

来源:百度知道 编辑:UC知道 时间:2024/09/28 14:31:29
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
unsigned int sjs;
int a,b,c,d;
sjs=rand()%10000;
while(sjs<1000)
{
sjs=rand()%10000;
d=sjs%10;
c=(sjs/10)%10;
b=(sjs/100)%10;
a=(sjs/1000)%10;
while(a==b||b==c||c==d||b==d||a==c||a==d)
{
sjs=rand()%10000;
while(sjs<1000)
sjs=rand()%10000;
}
}
printf("***********************************************\n\n");
printf("The number have been came out%d\n",sjs);
printf("\n***********************************************\n");
}
这是一段关于产生四位随机数的源程序,但不知道为什么每次产生的数都是一样的,有没有办法让每次产生的四位随机数都不一样?望各位指点指点~~~怎样进行修改才能使每次产生的四位随机数都不一样,最好加上说明,因为我只是个新手,对随机数这块不怎么清楚~~~谢谢哈!
怎么给不同的种子阿,能不能详细点?

#include<stdlib.h>
#include<time.h>
前面有过头文件了,不说了
srand(time(NULL));
rand() % 100;
这两句可以设置0~99随机数,可以自己更改 srand(time(NULL));这句time(NULL)意思是以时间为种子,而每次运行时时间都是不一样的,懂了吧(*^__^*) 嘻嘻……rand() % 100;后面%100是取余运算,则不用讲了,需要多大自己更改吧!!!要四位的话就是rand() % 10000;就ok了!!!

rand函数给他不同的种子,他就会产生不同的了.

你的while太多了,会搞混的,有些根本没必要

srand(time(NULL));

把这个srand((unsigned)time(NULL)); 加在
int a,b,c,d;
后面