Thread: Blackjack code.

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Blackjack code.

    Hello. So I set to write a simple Blackjack program, and what I have done so far (not the whole code) works fine except that the rand() function keeps giving out the same values over and over. So it's not really random, and I don't know what I'm doing wrong. Will you take a look at it?

    Code:
    #include<stdio.h>
    
    int main(void){
        int n,i,k,cont;
        char card;
        
        for(n=0;n<20;n++){
                          cont=0;
                          printf("#####################################\n");
                          printf("             BLACKJACK\n\n");
                          
                          do{
                             printf("Nova carta?\n");
                             scanf("%c",&card);
                             
                             if(card == 's'){
                                  cont+=rand()%12;
                                  if(cont>21){
                                              printf("%d",cont);
                                              printf("Temos pena mas perdeu!!\n");}
                                              
                                  if(cont==21){
                                               printf("BLACKJACK!!!");}
                                               
                                  else{
                                       printf("%d\n",cont);}
                                              }
                                  }while(card!='n' && cont < 22);
                                  
                          system("pause");
                          system("cls");
                          }
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try putting ...
    Code:
    #include <time.h>
    at the top of the file
    then add
    Code:
    srand(time(NULL));
    above your for loop....

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is a FAQ on random numbers. You should read it. Seed rand once at the very beginning of your program, and you should be fine.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by quzah View Post
    There is a FAQ on random numbers. You should read it. Seed rand once at the very beginning of your program, and you should be fine.


    Quzah.
    Thanks! That helped a lot. I thought I just needed to plug in the rand() function and the whole thing would just work. Thanks for addressing me to the FAQ.

    Quote Originally Posted by CommonTater View Post
    Try putting ...
    Code:
    #include <time.h>
    at the top of the file
    then add
    Code:
    srand(time(NULL));
    above your for loop....
    Thanks. That works indeed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM