Jump to content
Fórum Script Brasil
  • 0

Problema Com O Rand() E O Srand()


Inufelipe
 Share

Question

Ola galera do script brasil forum.

Eu estou a criar um joguinho

de cartas( ou cromos ), e estou

usando o rand() e o srand(time(NULL))

para criar as cartas aleatoriamente

e seus atributos, o problema é

que não esta muito aleatorio

porque as cartas se repetem,

se alguém puder ajudar

a tornar mais aleatorio

eu agradeço.

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>

int ra(){
    int temp;
    temp = rand();
    return temp;
}

void direction(int a, int b){
    switch(b){
        case 1:
            printf("Toda:");
            break;
        case 2:
            switch(a){
                case 0:
                    printf("Topo e Esquerda:");
                    break;
                case 1:
                    printf("Base e Direita:");
                    break;
            }
            break;
        case 4:
            switch(a){
                case 0:
                    printf("Topo:");
                    break;
                case 1:
                    printf("Esquerda:");
                    break;
                case 2:
                    printf("Base:");
                    break;
                case 3:
                    printf("Direita:");
                    break;
            }
            break;
    }
}

class Cromo{
    private:
        int colors;
        int *color;
        bool field;
        
    public:
        bool hand;
        Cromo(){}
        void set(int a){
            colors = 1;
            hand = 0;
            field = 0;
            color = (int*)malloc(sizeof(int));
            *color = a;
        }
        void set(int a,int b){
            colors = 2;
            hand = 0;
            field = 0;
            color = (int*)malloc(sizeof(int)*2);
            color[0] = a;
            color[1] = b;
            
        }
        void set(int a,int b,int c, int d){
            colors = 4;
            hand = 0;
            field = 0;
            color = (int*)malloc(sizeof(int)*4);
            color[0] = a;
            color[1] = b;
            color[2] = c;
            color[3] = d;
            
        }
        void random(){
            int i=0;
            int temp;
            temp = (ra()%3)+1;
            switch(temp){
                case 1:
                    srand(time(NULL));
                    set(ra()%5);
                    break;
                case 2:
                    srand(time(NULL));
                    set(ra()%5,ra()%5);
                    break;
                case 3:
                    srand(time(NULL));
                    set(ra()%5,ra()%5,ra()%5,ra()%5);
                    break;
            }
        };
        void status(int j){
            int i = 0;
            printf("\n%d:",j);
            while(i<colors){
                switch(color[i]){
                    case 0:
                        printf("\n\t");
                        direction(i,colors);
                        printf("Vermelho");
                        break;
                    case 1:
                        printf("\n\t");
                        direction(i,colors);
                        printf("Amarelo");
                        break;
                    case 2:
                        printf("\n\t");
                        direction(i,colors);
                        printf("Verde");
                        break;
                    case 3:
                        printf("\n\t");
                        direction(i,colors);
                        printf("Azul");
                        break;
                    case 4:
                        printf("\n\t");
                        direction(i,colors);
                        printf("Branco");
                        break;
                }
                i++;
            }
        }
};

class Table{
    private:
        Cromo table[6][3];

    public:
        Table(){

        }
};

class Book{
    private:
        int cards;
        Cromo *card;
        int top_card;
    public:
        Book(){

        }
        void set( int a ){
            int i;
            top_card = 0;
            cards = a;
            card = (Cromo *)malloc(sizeof(Cromo)*a);
            for(i=0;i<a;i++){
                card[i].random();
            }
            buy();
            buy();
            buy();
        }
        void buy(){
            card[top_card++].hand = 1;
        }
        void show_hand(){
            int i;
            while(i<cards){
                if(card[i].hand == 1){
                    card[i].status(i);
                }
                i++;
            }
        }
};

class Player{
    private:
        Book book;
    public:
        Player(){
            book.set(25);
        }
        void turn(){
            printf("Mao:\n\n");
            book.show_hand();
            getchar();
            book.buy();
        }
};

int main(){
    srand(time(NULL));
    Player p1,p2;
    bool turn = 0;
    while(1){
        if(turn == 0){
            p1.turn();
            turn = 1;
        }
        if(turn == 1){
            p2.turn();
            turn = 0;
        }
    }
};

falous

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
Guest Visitante

tente colocar apenas um srand(time(NULL)) em seu codigo.

tipow:

tire todos os srand() do seu codigo deixando apenas o do main.... isso deve funcionar a deixar mais aleatorio.

abraços.

Link to comment
Share on other sites

  • 0

Evite fazer rand()%n para qualquer n.

Isso é pessimo, pois pega os bits menos significativos do numero.

O ideal é fazer:

(rand() / (RAND_MAX + 1.0))*n
onde n é o primeiro valor que não entra na parada... Ou seja,
(rand() / (RAND_MAX + 1.0))*15

gera numeros double entre [0,15)

como você precisa de inteiros basta guardar num int para dar o cast...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.8k
    • Total Posts
      646.6k
×
×
  • Create New...