我有以下代码。我想要完成的是从头文件中的所有数组中“生成”一个随机名称/地址/Zip。问题是我任何时候都得到相同的地址,所以我做错了。
xtmtrx@server:~/coding/c/work$ ./randaddr
Olivier|Suh|28 King Street Apt 21|11934
xtmtrx@server:~/coding/c/work$ ./randaddr
Olivier|Suh|28 King Street Apt 21|11934
xtmtrx@server:~/coding/c/work$ ./randaddr
Olivier|Suh|28 King Street Apt 21|11934
xtmtrx@server:~/coding/c/work$
randaddr.c
#include <stdio.h>
#include <stdlib.h>
#include "first.h"
#include "last.h"
#include "streets.h"
#include "zips.h"
int main() {
const size_t firsts_count = sizeof(firsts) / sizeof(firsts[0]);
const size_t lasts_count = sizeof(lasts) / sizeof(lasts[0]);
const size_t streets_count = sizeof(streets) / sizeof(streets[0]);
const size_t zips_count = sizeof(zips) / sizeof(zips[0]);
printf("%s|%s|%s|%s\n", firsts[rand() % firsts_count], lasts[rand() % lasts_count], streets[rand() % streets_count], zips[rand() % zips_count]);
return 0;
}
第一个.h
const char *firsts[] = {
"Asgar",
"Aadit",
"Aanand",
"Aaron",
"Aarti",
"Aasha",
"Abby",
"Abdel",
"Abdoulaye"
};
最后一个.h
const char *lasts[] = {
"ALI",
"ALZAYED",
"ARSAN",
"Abdelmaseeh",
"Abdulla",
"Abebe",
"Aber",
"Abeshi",
"Abraham"
};
街道.h
const char *streets[] = {
"1000 Park Ave",
"1001 Park Avenue",
"1003 St. Nicholas Ave.",
"100 Bleecker St",
"100 E31st St",
"100 East 68th Street Apt. 14E",
"100 Jane St.",
"100 Jane St PH-J",
"100 John St. Apt 2208"
};
zips.h
const char *zips[] = {
"00501",
"00544",
"06390",
"10001",
"10002",
"10003",
"10004",
"10005",
"10006"
};