Create function that returns random numbers in C

Asked

Viewed 2,518 times

1

I need to create a function that returns random numbers without repetition between 2 intervals.

I wanted to create a kind of a card game, where the cards are shuffled, and when they return they never come out again.

In my code is repeating the values.

Code removed for privacy reasons

  • 1

    Hi Marconi. Why did you delete the question, even with such a complete answer? By the way, you have already deleted several questions! Remember that the content posted here on the site is not for specific users, but for the entire internet. An answer that solves your problem can also solve other people’s. We moderators talked and decided to undo the deletion of this question, okay? From now on, please think twice before deleting a post. If you would like to discuss the matter, we are available. Thank you.

  • @bfavaretto Questions that exclude or have not been answered, or have been poorly planned. I apologize. Before deleting them I will rethink the questions.

  • Okay Marconi, thank you for understanding.

1 answer

2


Random function

I imagine you just want to use a function like this. Creating one is not a very simple task. better use what is already ready.

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

int main(void) {
    srand(time(NULL));
    int r = rand() %13; //é número de cartas de cada naipe
    printf("Número sorteado %d", r);
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Documentation of the function rand() and srand() can be found here.

Algorithm without repetition

I believe the complete solution you are accurate is based on the algorithm Fisher-Yates. Something like that (if I agree with that response in the OS):

int rand_int(int n) {
    int limit = RAND_MAX - RAND_MAX % n;
    int rnd;

    do {
        rnd = rand();
    } while (rnd >= limit);
    return rnd % n;
}

void shuffle(int *array, int n) {
    int i, j, tmp;

    for (i = n - 1; i > 0; i--) {
        j = rand_int(i + 1);
        tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }
}

So now is to adapt to your need.

Deck

I found an example more or less ready for what you want on that page.

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

#define CARDS 52
#define DRAW 5

void showcard(int card);

int main() {
    int deck[CARDS];
    int c;

/* initialize the deck */
    for (int x = 0; x < CARDS; x++) deck[x] = 0;
    srand((unsigned)time(NULL));
    for (int x = 0; x < DRAW; x++) {
        for(;;) {                 /* loop until a valid card is drawn */
            c = rand() % CARDS;     /* generate random drawn */
            if(deck[c] == 0) {       /* has card been drawn? */
                deck[c] = 1;        /* show that card is drawn */
                showcard(c);        /* display card */
                break;              /* end the loop */
            }
        }                       /* repeat loop until valid card found */
    }
}

void showcard(int card) {
    char *suit[4] = { "Spades", "Hearts", "Clubs", "Diamonds" };
    switch (card % 13) {
        case 0:
            printf("%2s","A");
            break;
        case 10:
            printf("%2s","J");
            break;
        case 11:
            printf("%2s","Q");
            break;
        case 12:
            printf("%2s","K");
            break;
        default:
            printf("%2d",card%13+1);
    }
    printf(" of %s\n",suit[card/13]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • @Marconi Rand + modulus = fail => take a reasonably modern C++ compiler, make your life easier, use something politically correct. Study.

  • @pepper_chico know very little about C. I know more about Asp.net C#.

  • @Marconi do not worry, you are doing a school exercise and not a system to control the central bank. But it doesn’t even matter, there are people who like a language and stand up for it even if it doesn’t bring any real advantage.

  • @bigown Thanks.

  • @Marconi highly recommend that you read the first link, pure C. The other is to facilitate your life only, you can implement uniform distribution in C as well, or use any lib with uniform distribution of integers correctly. It’s good that you know this, I myself have had a good time not being aware of this when it comes to it. A rigged game or addicted encryption is a gateway for hackers. The advantage [fact] of C++ is that this is already in the standard lib.

  • @Marconi Rand + module is as infamous as the gets function.

  • @Okay, thank you very much.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.