C Language: Simple Random Number Generator


Random number generator in C with srand() and time(). Srand() is a helper function for the rand() function, generating a seed for the start of a sequence of (pseudo)random numbers. Because the time returned by the time() function changes once a second, when calling this code more often, there will be no randomness. To be – use nanoseconds through timespec_get ().

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

int main(void)
{
    int n, r;

    printf("Rng at your service:\n\n");
    srand((unsigned)time(NULL));

    for (n = 0; n < 10; n++)
    {
            r = rand();
            printf("%d\t", r % 100);
    }

    return 0;
}

This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.