Code to generate random number sequence does not work

Asked

Viewed 196 times

2

I developed a C code to generate a sequence of random numbers to be typed by the user, but it’s the first time I make a source code of the type for a college job, and according to the research I did I was able to come up with an idea:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
  int i;
  int quant_random; //Quantidade de aleatórios gerados

  inicio:
  printf("Escreva uma quantidade de numeros aleatorios a serem gerados: ");
  scanf("%d", quant_random);
  if (quant_random <= 0 || quant_random > 100)
  {
      printf("\nValor incorreto, digite um numero entre 1 e 100");
      goto inicio; //Estou usando esse goto para levar ele para a frase de novo quando a pessoa digitar um valor incorreto
  }

  else
  {

        srand(100); //Modelo que encontrei em site como base, só não estou entendendo a lógica de como ele funciona
        for (i = 0; i < 5; i++)
        {
        /* gerando valores aleatórios entre zero e 100 */
        printf("\n\n%d ", rand() % 100);
        }

  getch();
  }

    return 0;
}

When executing it, however, it asks to enter as a value, but under both conditions: entering a code in the range from 1 to 100 or even outside this scope, it does not show any return. Because as I put in if it is less than or equal to 0 or greater than 100 it should return to the initial message through the goto. Someone can understand the logic to hit him?

The statement says that the number typed by the user is the amount to be random numbers to be generated.Deveria entrar no if e o loop levar ele para a primeira fase novamente

  • 1

    Missed the & in scanf("%d", quant_random);. Note how you are starting from a fixed seed (srand(100);) the sequence of pseudo-random numbers generated will always be the same every time you run the program. Avoid using droplet.

2 answers

1


Code:

#include <stdio.h>
#include <stdlib.h>
#include<time.h> //para gerar numeros aleatorios
int main()
{
  int i;
  int quant_random; //Quantidade de aleatórios gerados

  srand(time(0)); //de 100 para time(0)

  printf("Escreva uma quantidade de numeros aleatorios a serem gerados: ");
  scanf("%d", &quant_random);
  while(quant_random <= 0 || quant_random > 100)
  {
    printf("\nValor incorreto, digite um numero entre 1 e 100");
  }
  for(int i = 0; i < quant_random; i++){
        /* gerando valores aleatórios entre zero e 100 */
        printf("\n\n%d ", rand() % 100);   
  }

    return 0;
}

Explanation:

First I removed the goto.

The reason people say to avoid the drop is legibility. It would be to facilitate the understanding of the code, so the programmer does not get lost in what the code does. The goto itself does not cause any problem.

The goto still exists because it is still useful. It exists because most language creators are pragmatic.

Then I changed your srand(), I will not explain in detail here, because it has this very good explanation

But I’ll give you a summary if you don’t put the srand() it will always generate the same sequence of numbers, this sequence changes from computer to computer, but no matter how many times you run it will be the same.

With a fixed value like 100 he will use the 100 as a seed(seed), and will generate the same number ALWAYS, now with the time(0) it basically takes the current number of seconds according to the time stamp UNIX.

Thus causing the code to generate pseudo random numbers.

The while is to check if the number typed is between 1-100 and the for to show the desired amount of random numbers.

I put it in the Repl.it for future reference

  • Thanks for the help, I read the article of geeks for geeks and following your explanation I also managed to understand the logic behind and solved the problem, I redid the code in its form and worked perfectly, thank you.

  • @Gabriel0598 Glad you liked the answers! But the best way to thank those who helped you is to mark "accept" the best answer and vote for all who helped you. So you make sure that whoever wrote the answer gets something in return, in addition to making the site cleaner and more useful for everyone.. :)

-1

Your program will only show 5 numbers, because you had the loop repeat 5 times. It should pass

 for (i = 0; i <= quant_random; i++);

Here the amount of times the loop will repeat is defined in quant_random

  • But even with this it will not generate random numbers, it will always generate the same number.

Browser other questions tagged

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