Create a function that returns random number in C

Asked

Viewed 556 times

-4

Good afternoon, I have a project in language c to deliver, and it’s almost all ready, but the teacher said q we have to use a function that returns random number, but the random function I have to create, only I have no idea is nor do I see how to do, already thank you.

  • 2

    Leo, the goal here is to teach you how to fish and not fish for you. Try to build something with what you already know. As you encounter problems, you can ask for help here, the community will be happy to help.

2 answers

1

You can try using a function that generates random numbers.

For example:

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

int main(void)
{
     int i;

     printf("Gerando 10 valores aleatorios:\n\n");

     for (i = 0; i < 10; i++)
     {
           /* gerando valores aleatórios entre zero e 100 */
           printf("%d ", rand() % 100);
     }

     getch();
     return 0;
}

-1

The C language itself provides a function for generating "random" (pseudo random) numbers. To make the number "more random", you can use the time to sow the function.

Required headings:

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

Code:

srand (time(NULL));
rand();

Read more on cplusplus.

Browser other questions tagged

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