您的位置:首页 > 其它

生成四位随机数,数字不重复

2012-11-16 23:44 225 查看
发现以前写的算法,过段时间不去看竟然忘得一干二净。

使用c语言写的,算是做个记录,免得以后忘记。

#include <math.h>
#include <time.h>
#include <stdlib.h>
int GetRand()
{
int sum = 4,number = 0,tem = 0;
int arrr[10];
memset(arrr,-1,10 * 4);
bool isFirst = true;
srand(time(NULL));
while (sum > 0)
{
tem = rand() % 10;
if (arrr[tem] == -1)
{
if (tem == 0)
{
if (isFirst);
else
{
arrr[tem] = tem;
sum --;
}
}
else
{
arrr[tem] = tem;
number += tem * pow(10.0,sum - 1);
isFirst = false;
sum --;
}
}
}
return number;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: