randomNumberFill

→  stdio + stdlib 常用函數


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

// 生成n位隨機數
int randomNumberFill(char *buffer,int count){
    int res;
    int i=0;
	srand(time(0));
	for(;i<count;i++)
	{
		buffer[i]=(char)(rand()%10+48);
		/*重複檢測*/
		int k=0;
		while(k<i){
			if(buffer[k] == buffer[i]){
				buffer[i] = (char)(rand()%10+48);
				k = 0;
				continue;
			}else{
				k++;
				if(k == count){
					break;
				}
			}
		}
	}	
}

int main(){
    char a[6];
    randomNumberFill(a,6);
    int i=0;
    int b = atoi(a);  
    printf("%d\n",b);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章