Random generation

Asked

Viewed 61 times

-2

Hello. I was asked a question in a random number study in C. How would I generate a value in an interval that is not continuous? Generate a value between 10 to 15 or 20 to 25, for example.

  • See if this question helps you: https://answall.com/questions/69332/gerar-n%C3%bameros-aleat%C3%b3rios-a-partir-de-um-conjunto-pr%C3%A9-defined or essa https://answall.com/questions/210172/gerarn%C3%bameros-aleat%C3%b3rios-em-c

2 answers

2

Basically, you add the length of the intervals and then spread the number in the given intervals.

For example, the range of 10 to 15 has 6 numbers (counting the 10 and the 15) and the 20 to 25 has six more numbers. So you have a total of 12 numbers to choose from. So you draw a number from 0 to 11 and if it’s between 0 and 5, you add 10, producing a number from 10 to 15. If it’s between 6 and 11, you add 14, producing a number from 20 to 25.

For example:

#include <time.h>

int numero_10a15_e_20a25() {
    srand(time(NULL));
    int x = rand() % 12;
    return x + (x < 6 ? 10 : 14);
}

int main(void) {
    printf("%d", numero_10a15_e_20a25());
}

See here working on ideone.

Another way to do it is by using an array to map the value from 0 to the amount of elements minus 1 for any set of numbers. For example:

#include <time.h>

#define TAMANHO_DO_ARRAY(x) (sizeof (x) / sizeof *(x))

int numero_faixa(int *faixa, int quantidade) {
    srand(time(NULL));
    return faixa[rand() % quantidade];
}

int numero_10a15_e_20a25() {
    int faixas[] = {10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25};
    return numero_faixa(faixas, TAMANHO_DO_ARRAY(faixas));
}

int main(void) {
    printf("%d", numero_10a15_e_20a25());
}

See here working on ideone.

  • Thank you very much. I had not realized that you can solve this by giving this "treated" in generation values. What mass. Thanks!

-1

Have you ever thought about doing this procedure in chain?

I don’t know C, so I’m not going to risk it, but I’m going to try to pass the logic involved, and then you just go to C and use Rand...

var = numero-aleatorio (1 a 3);

Se (var == 1){
  num = numero-aleatorio (10 a 15);
}

Se (var == 2){
  num = numero-aleatorio (20 a 25);
}

Se (var == 3){
  num = numero-aleatorio (30 a 35);
}

Mostre(num);

To complement, I decided to make the practical example:

#include <stdio.h>

// Funçao para gerar os numeros aleatórios
int numeroAleatorio(){
    // Inicializa o gerador de números aleatórios
    srand(time(NULL));

    // Faz o primeiro sorteio, para saber opção de numero vai usar
    int p = rand() % 3;

    // Se for a opção 1
    if(p == 0)
        // Retorne de 10 a 15
        return 10 + (rand() % 5);
    // Se for a opção 2
    if(p == 1)
        // Retorne de 20 a 25
        return 20 + (rand() % 5);
    // Se for a opção 3
    if(p == 2)
        // Retorne de 30 a 35
        return 30 + (rand() % 5);
}

int main(void) {
    // Chama a funçao numero aleatório e a imprime na tela
    printf("Numero Sorteado: %d", numeroAleatorio());
}

Ideone example

Source Searched 1

Source Researched 2

  • I just wanted to thank Victor Stafusa, I didn’t know ideone, thanks to this tool I was able to make the practical example. Vlw! I commented here because unfortunately I can not comment on your reply.

Browser other questions tagged

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