[英]Why doesn't this print a random number?
此代碼有什么問題?
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
void createNumb(int RandomNumber) {
srand(time(NULL));
int a, b, c, d;
a = rand() % 10 + 0;
b = rand() % 10 + 0;
c = rand() % 10 + 0;
d = rand() % 10 + 0;
while (b == a) {
b = rand() % 10 + 0;
}
while (c == b || c == a) {
c = rand() % 10 + 0;
}
while (d == c || d == b || d == a) {
d = rand() % 10 + 0;
}
RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
}
int main() {
int x;
createNumb(x);
printf("%d", x);
}
當我運行該程序時,它總是給我結果0
。
我希望它打印一個隨機的4位數字(每個數字必須是唯一的)。 問題是什么?
createNumb
函數返回void
而不是int
。 srand()
一次 ,因此最好將其移至main
。 您的createNumb
函數應返回一個隨機整數 ,如下所示:
int createNumb(void) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
return rand();
}
int main(void) {
srand(time(NULL))(
int x = createNumb();
printf("%d" , x);
}
或者,如果要createNumb
填充現有整數 ,請使用指針 :
void createNumb(int *randomNumber) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
*randomNumber = rand();
}
int main(void) {
int x;
srand(time(NULL))
createNumb(&x); // Passing a pointer to x
printf("%d" , x);
}
您的C代碼中存在一些問題,它們來自另一種語言:
return
從函數返回值,並在函數名之前指定返回類型。 RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
使用運算符,
該運算符將忽略其左操作數,並求值到右操作數,其值會自動存儲到存儲在本地變量中,並在函數退出時丟失。 只需使用return a * 1000 + b * 100 + c * 10 + d * 1;
。 rand() % 10000
,但是可能需要更多的位數或特定約束(例如唯一位數)之類的方法。 srand()
使用變化更快的源,例如clock()
並在將main()
函數初始化一次后就初始化偽隨機數發生器。 printf
格式"%04d"
使顯示的首字母為0
。 這是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d;
a = rand() % 10;
b = rand() % 10;
c = rand() % 10;
d = rand() % 10;
while (b == a) {
b = rand() % 10;
}
while (c == b || c == a) {
c = rand() % 10;
}
while (d == c || d == b || d == a) {
d = rand() % 10;
}
return a * 1000 + b * 100 + c * 10 + d;
}
int main() {
int x;
srand(clock());
x = createNumb();
printf("%04d\n", x);
return 0;
}
請注意,您可以避免creatNumb()
的循環。 這是一個修改后的版本,它從命令行使用一個參數來產生多個結果。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
return a * 1000 + (b + bb) * 100 + (c + cc) * 10 + (d + dd);
}
int main(int argc, char *argv[]) {
int x, n;
srand(clock());
n = (argc > 1) ? strtol(argv[1], NULL, 0) : 1;
while (n-- > 0) {
x = createNumb();
printf("%04d\n", x);
}
return 0;
}
布爾運算符的值為true時為1
,否則為0
。 bb = (b >= a);
是以下內容的縮寫:
bb = 0;
if (b >= a)
bb = 1;
如果要計算具有給定約束的4個數字的數組,則應將指向目標數組的指針作為creatNumb()
的參數傳遞。 這是使用此方法的修改版本:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void createNumb(int *dest) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
dest[0] = a;
dest[1] = b + bb;
dest[2] = c + cc;
dest[3] = d + dd;
}
int main(int argc, char *argv[]) {
int array[4];
srand(clock());
createNumb(array);
printf("%d%d%d%d\n", array[0], array[1], array[2], array[3]);
return 0;
}
注意,將數組作為參數傳遞給函數實際上是將指針傳遞給函數的第一個元素: createNumb(array);
等效於createNumb(&array[0]);
。 在大多數表達式上下文中 ,此過程稱為數組衰減為指針 ,這對於初學者而言有些令人困惑,但允許有效的參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.