最近在寫學校作業時,發現一個有關亂數的問題,來請各位大大幫我看一下
程式碼:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include <cstdio>
using namespace std;
struct student {
string name;
string id;
string address;
string birthday;
};
int num;
int seed();
void Displaydata(student[], int);
string randomname();
string randomid();
string randaddress();
string randbirthday();
student getdata();
int main() {
cout << "請輸入學生人數,程式會隨機產生學生的資料\n";
cin >> num;
student* p;
p =new student[num];
for (int i = 0; i < num; i++) {
p[i]= getdata();
}
Displaydata(p, num);
system("pause");
}
int seed(void) {
int a;
srand(time(NULL));
a= rand() % 1000 + 1;
return a;
}
string randomname(void) {
srand(time(NULL)+seed());
int lenth;
lenth = rand ()% 5 + 1;
string s;
static const char alphanum[] =
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randomid(void) {
srand(time(NULL) + seed());
int lenth = 6;
string s;
static const char alphanum[] =
"0123456789";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randaddress(void) {
srand(time(NULL) + seed());
int lenth = 1;
string s;
static const char alphanum[] =
"ABCDEFGHIJKMNOPQTUVWXZ";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randbirthday(void) {
srand(time(NULL) + seed());
int lenth = 8;
string s;
static const char alphanum[] =
"0123456789";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
void Displaydata(student p[], int num) {
int i;
for (i = 0; i <num; i++) {
cout << "\n";
cout << "姓名:" << p[i].name << "\n";
cout << "ID:" << p[i].id << "\n";
cout << "地址:" << p[i].address << "\n";
cout << "生日:" << p[i].birthday << "\n";
cout << "\n";
}
}
student getdata(void) {
student newstudent;
newstudent.name=randomname();
newstudent.id = randomid();
newstudent.address=randaddress();
newstudent.birthday = randbirthday();
return newstudent;
}
輸出結果:
請輸入學生人數,程式會隨機產生學生的資料
3
姓名:qyjij
ID:966563
地址:O
生日:96656345
姓名:qyjij
ID:966563
地址:O
生日:96656345
我想問的問題是,為甚麼跑出來的三個資料是一樣的?
有沒有甚麼方法可以讓結果不一樣?
麻煩大家了~~
srand 正常而言一份程式碼(專案)只能執行一次,
如果它放在 for loop 裡,
每次進行 rand 前就用 srand,
會發現每次取出來的亂數是同一個數字
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
main() {
int max;
max = 100; //設隨機最大數(用餘數法取隨機數)
srand(time(0));
for(int i = 0; i<10; i++) { //丟10次
cout << "The random number is: "<<rand()%max << endl;
}
}
在您的程式碼中,您將 seed() 函數設計成回傳一個隨機數字,然後在各個亂數產生函數中都使用 srand() 函數來設定亂數種子,以便每次產生的亂數序列都不同。但是,每次在呼叫 srand() 函數時,您都是使用 time() 函數的回傳值作為亂數種子。由於 time() 函數的精度是秒級的,因此在同一秒鐘內呼叫多次 srand() 函數,所產生的亂數序列仍然是相同的。
為了解決這個問題,您可以將 seed() 函數改成不回傳任何值,而是直接在函數內呼叫 srand() 函數,使用 time() 函數的回傳值作為亂數種子。這樣每次呼叫 seed() 函數時,都會使用不同的亂數種子,從而產生不同的亂數序列。
以下是修改後的程式碼:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include <cstdio>
using namespace std;
struct student {
string name;
string id;
string address;
string birthday;
};
int num;
void Displaydata(student[], int);
string randomname();
string randomid();
string randaddress();
string randbirthday();
void seed();
student getdata();
int main() {
cout << "請輸入學生人數,程式會隨機產生學生的資料\n";
cin >> num;
seed();
student* p = new student[num];
for (int i = 0; i < num; i++) {
p[i] = getdata();
}
Displaydata(p, num);
system("pause");
}
void seed() {
srand(time(NULL));
}
string randomname() {
int lenth = rand() % 5 + 1;
string s;
static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randomid() {
int lenth = 6;
string s;
static const char alphanum[] = "0123456789";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randaddress() {
int lenth = 1;
string s;
static const char alphanum[] = "ABCDEFGHIJKMNOPQTUVWXZ";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
string randbirthday() {
int lenth = 8;
string s;
static const char alphanum[] = "0123456789";
for (int i = 0; i < lenth; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
void Displaydata(student p[], int num) {
for (int i = 0; i < num; i++) {
cout << "\n";
cout << "姓名:" << p[i].name << "\n";
cout << "ID:" << p[i].id << "\n";
cout << "地址:" << p[i].address << "\n";
cout << "生日:" << p[i].birthday << "\n";
cout << "\n";
}
}
student getdata() {
student newstudent;
newstudent.name = randomname();
newstudent.id = randomid();
newstudent.address = randaddress();
newstudent.birthday = randbirthday();
return newstudent;
}