-
randomize problem..
now i'm doing a mastermind homework..
my problem is how can i randomize once only when the program is running..
Code:
nine()
{
int f,s,t,fo,fi,si,sev,ei,nin,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,r,p;
f=(rand()%10);
s=(rand()%10);
t=(rand()%10);
fo=(rand()%10);
fi=(rand()%10);
si=(rand()%10);
sev=(rand()%10);
ei=(rand()%10);
nin=(rand()%10);
r=0;
p=0;
printf("%d%d%d%d%d%d%d%d%d",f,s,t,fo,fi,si,sev,ei,nin);
printf("Please input your guess(In One Digit)");
printf("\nFirst\n");
scanf("%d",&first);
printf("Second\n");
scanf("%d",&second);
printf("Third\n");
scanf("%d",&third);
printf("Fourth\n");
scanf("%d",&fourth);
printf("Fifth\n");
scanf("%d",&fifth);
nine()
{
int f,s,t,fo,fi,si,sev,ei,nin,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,r,p;
f=(rand()%10);
s=(rand()%10);
t=(rand()%10);
fo=(rand()%10);
fi=(rand()%10);
si=(rand()%10);
sev=(rand()%10);
ei=(rand()%10);
nin=(rand()%10);
r=0;
p=0;
printf("%d%d%d%d%d%d%d%d%d",f,s,t,fo,fi,si,sev,ei,nin);
printf("Please input your guess(In One Digit)");
printf("\nFirst\n");
scanf("%d",&first);
printf("Second\n");
scanf("%d",&second);
printf("Third\n");
scanf("%d",&third);
printf("Fourth\n");
scanf("%d",&fourth);
printf("Fifth\n");
scanf("%d",&fifth);
if(third==t)
r=r+1;
else if(third==f||third==s||third==fo||third==fi||third==si||third==sev||third==ei||third==nin)
p=p+1;
else if(!(third==t||third==f||third==s||third==fo||third==fi||third==si||third==sev||third==ei||thir
d==nin))
{}
if(fourth==fo)
r=r+1;
else if(fourth==f||fourth==s||fourth==t||fourth==fi||fourth==si||fourth==sev||fourth==ei||fourth==ni
n)
p=p+1;
else if(!(fourth==f||fourth==s||fourth==t||fourth==fi||fourth==si||fourth==sev||fourth==ei||fourth==
nin))
{}
if(fifth==fi)
r=r+1;
else if(fifth==f||fifth==s||fifth==t||fifth==fo||fifth==si||fifth==sev||fifth==ei||fifth==nin)
p=p+1;
else if(!(fifth==fi||fifth==f||fifth==s||fifth==t||fifth==fo||fifth==si||fifth==sev||fifth==ei||fift
h==nin))
{}
if(sixth==si)
r=r+1;
else if(sixth==f||sixth==s||sixth==t||sixth==fo||sixth==fi||sixth==sev||sixth==ei||sixth==nin)
p=p+1;
else if(!(sixth==si||sixth==f||sixth==s||sixth==t||sixth==fo||sixth==fi||sixth==sev||sixth==ei||sixt
h==nin))
{}
if(seventh==sev)
r=r+1;
else if(seventh==f||seventh==s||seventh==t||seventh==fo||seventh==fi||seventh==si||seventh==ei||seve
nth==nin)
p=p+1;
else if(!(seventh==f||seventh==s||seventh==t||seventh==fo||seventh==fi||seventh==si||seventh==ei||se
venth==nin||seventh==sev))
{}
if(eighth==ei)
r=r+1;
else if(eighth==f||eighth==s||eighth==t||eighth==fo||eighth==fi||eighth==si||eighth==sev||eighth==ni
n)
p=p+1;
else if(!(eighth==f||eighth==s||eighth==t||eighth==fo||eighth==fi||eighth==si||eighth==sev||eighth==
nin||eighth==ei))
{}
if(ninth==nin)
r=r+1;
else if(ninth==f||ninth==s||ninth==t||ninth==fo||ninth==fi||ninth==si||ninth==sev||ninth==ei)
p=p+1;
else if(!(ninth=ei||ninth==f||ninth==s||ninth==t||ninth==fo||ninth==fi||ninth==si||ninth==sev||ninth
==nin))
{}
if(r==9)
{printf("Congratulations,You Guess Right,You Are The Mastermind!!");
return 0;
}
printf("\n%d Right,%d Place Wrong\n",r,p);
return nine();
}
sorry if the code too long. :p ,anyone can help?
-
I don't understand your question, maybe you want to know why rand keeps returning the same value. Try putting this at the start of your main:
-
randomize problem
the problem is when i returned to nine() i will get a new random numbers from the f,s,t,fo,etc..
are there any solution to make the random function just generates once and then the values is saved to f..thx
-
Call rand() outside of nine() and pass that to nine(). Then use that argument for your calculations.
-
>>are there any solution to make the random function just generates once
Sure, just call it once. You'd need to make the variables static if you want to retain their values from one function call to the next.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void foo(void)
{
static int InitDone;
static int a;
if (InitDone == 0)
{
InitDone = 1;
a = rand() % 10;
}
printf("A = %d\n", a);
}
int main(void)
{
srand(time(NULL));
foo();
foo();
foo();
return(0);
}
-
Make those variables global and have a function to initialize them with the random numbers then only call the initialization function once.
-
>>Make those variables global
... and they need to be global because ... ?
In general terms, only use global variables if you need to share data between functions, and the design makes it hard to pass the variables as parameters.
-
randomize problem
yeah!it's worked!!thx for all who helped me,since i got a new problem though..when i'm stuck,i'll ask again.... :D ...