我正在制作一个小的 C 程序来类似于游戏代码破坏者或 Mastermind。到目前为止,这是我的代码。到目前为止,它所做的只是生成一个“随机”代码并打印代码的数组。每次我编译它时,它都会生成相同的 2、2、6、3 数组。谁能帮我有一个真正的随机数生成器?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define CODELENGTH 4
#define NUMSYMBOLS 6
const int LOW = 1;
const int HIGH = 6;
void genCode (int MasterCode[])
{
int i=0;
int k;
while (i < 4){
MasterCode[i] =rand() %(HIGH-LOW+1)+LOW;
i++;
}//end while loop.
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", MasterCode[ k ] );
}
printf( "\n" );
}
void getGuess (int guess[])
{
int b[ 4 ];
int number = 0;
printf( "Please enter your list of 4 numbers between 1 and 6: " );
int j;
int k;
for ( j = 0 ; j < 4; j++ ) {
scanf( "%d", &number );
b[ j ] = number;
}
printf( "Your array has these values: " );
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", b[ k ] );
}
printf( "\n" );
}
int main (int argc, char **argv)
{
int MasterCode[4];
genCode(MasterCode);
}