Thread: randomize problem..

  1. #1
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    Question 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. ,anyone can help?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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:

    Code:
    srand(time());

  3. #3
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    Question 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

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Call rand() outside of nine() and pass that to nine(). Then use that argument for your calculations.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Make those variables global and have a function to initialize them with the random numbers then only call the initialization function once.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.


    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    Thumbs up 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.... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM