iT邦幫忙

0

c++亂數問題

最近在寫學校作業時,發現一個有關亂數的問題,來請各位大大幫我看一下

程式碼:

#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

姓名:qyjij
ID:966563
地址:O
生日:96656345

我想問的問題是,為甚麼跑出來的三個資料是一樣的?
有沒有甚麼方法可以讓結果不一樣?
麻煩大家了~~

看更多先前的討論...收起先前的討論...
DanSnow iT邦好手 1 級 ‧ 2021-08-05 16:13:47 檢舉
不要一直 srand ,程式跑的很快,你的程式 1 秒內就可以跑完了,加上 time 的精度就只要到秒,這代表你產生亂數前,你都把亂數種子設定成同一個值,結果當然一樣
a7883925 iT邦新手 5 級 ‧ 2021-08-05 18:35:41 檢舉
那應該要怎麼改比較好啊??
10611014 iT邦新手 5 級 ‧ 2021-08-05 22:40:01 檢舉
能問一下各位大神要求擲出骰子總次數為1000次,用rand()求,但為何隨機出現的各個點數的總次數和為何不等於1000次,能說明一下或提供更好的解法嗎?
DanSnow iT邦好手 1 級 ‧ 2021-08-06 12:52:09 檢舉
@106110014 不要在別人的問題下問問題,要問自己開一個出來才比較好回,也比較有人回,你沒給程式碼,我這邊也不好貼程式碼,你覺得要別人怎麼回答
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
小魚
iT邦大師 1 級 ‧ 2021-08-06 00:02:58

srand 正常而言一份程式碼(專案)只能執行一次,
如果它放在 for loop 裡,
每次進行 rand 前就用 srand,
會發現每次取出來的亂數是同一個數字

a7883925 iT邦新手 5 級 ‧ 2021-08-06 01:07:02 檢舉

那我應該要怎麼改比較好啊??

1
japhenchen
iT邦超人 1 級 ‧ 2021-08-06 10:38:46
#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;
   }
}
看更多先前的回應...收起先前的回應...
a7883925 iT邦新手 5 級 ‧ 2021-08-06 11:52:30 檢舉

謝謝你的回答~~
但是好像跟我問的問題不太一樣,我想問的是要怎麼讓我的結果不會是一樣的

給srand(time(0))就能在每次產生rand時都會生出不同的數字

a7883925 iT邦新手 5 級 ‧ 2021-08-06 12:52:49 檢舉

0跟null不是一樣的嗎??

你的是 srand(time(NULL) + seed()); 出現的結果就不對了

a7883925 iT邦新手 5 級 ‧ 2021-08-06 15:09:20 檢舉

好的,我試試看

0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-14 09:23:35

在您的程式碼中,您將 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;
}

我要發表回答

立即登入回答