Mockingbird
Mockingbird

Reputation: 1031

Trick the randomizer in C

I want to get random numbers between 1 to 10. It actually works, but when it's in a loop, I don't really get random numbers.

int randomNum;
srand ( (unsigned int)time(NULL) );
randomNum = rand() % 10;

I've been spending hours here and in google looking for a solution, but it looks like no one really solved it (or maybe I didn't search good enough). The value we get from the randomizer depends on the seconds (not miliseconds or something else, like in other programming language) and that's why the numbers are not random.

In addition, I don't want to download a package for C because I run my code in the university labs, and they won't allow it.

Is there anyone with a creative solution for this problem? maybe some mathematic functions?

Upvotes: 3

Views: 274

Answers (3)

Binayaka Chakraborty
Binayaka Chakraborty

Reputation: 1335

Dave Newman provides a very good answer. Alternatively, you could also try a pseudo random generator, for example

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

int main()
{
    int a0; // this value will be our requirement
    int mod = 11; //this is the limit (0 - mod-1), here 10
    int a; // this stores the previous value of a0;
    int i; // loop variable
    int mul=25; //multiplicative factor
    int add=3; // additive factor
    int limit=100; // our limit
    srand ( (unsigned int)time(NULL) ); // initialize the seed
    a0 = rand() % mod;
    for(i=0;i<limit;i++)
    {
        printf("%d\t",a0);
        a = a0;
        a0 = (a * mul + add) % mod;
    }
    putchar('\n');
    return 0;
}

The output::


1st run::

2       10      4       3       1       8       0       6       7       9       2       10      4       3       1       8       0       6
7       9       2       10      4       3       1       8       0       6       7       9       2       10      4       3       1       8
0       6       7       9       2       10      4       3       1       8       0       6       7       9       2       10      4       3
1       8       0       6       7       9       2       10      4       3       1       8       0       6       7       9       2       10
4       3       1       8       0       6       7       9       2       10      4       3       1       8       0       6       7       9
2       10      4       3       1       8       0       6       7       9

2nd output::

9       2       10      4       3       1       8       0       6       7       9       2       10      4       3       1       8       0
6       7       9       2       10      4       3       1       8       0       6       7       9       2       10      4       3       1
8       0       6       7       9       2       10      4       3       1       8       0       6       7       9       2       10      4
3       1       8       0       6       7       9       2       10      4       3       1       8       0       6       7       9       2
10      4       3       1       8       0       6       7       9       2       10      4       3       1       8       0       6       7
9       2       10      4       3       1       8       0       6       7

Upvotes: 1

Dave Newman
Dave Newman

Reputation: 1016

To illustrate Sidoh's answer.

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

int main(int argc, char** argv)
{
    int i;

    srand ( (unsigned int)time(NULL) );

    for (i = 0; i < 100; i++)
    {
        printf("%d ", 1 + (rand() % 10));
    }
    putchar('\n');
    return 0;
}

This produced the following results for my one time seed using time( ).

7 10 2 4 4 4 2 1 7 7 10 4 3 10 2 9 6 9 2 9 7 10 4 1 1 8 2 4 8 1 2
4 2 3 9 5 8 1 7 4 9 8 10 1 8 1 1 5 1 4 5 7 3 9 10 3 6 1 9 3 4 10
8 5 2 7 2 2 9 10 5 9 8 4 1 7 7 2 3 7 5 8 6 10 8 5 4 3 7 2 8 2 1 7
7 5 5 10 6 5 

Upvotes: 5

sidoh
sidoh

Reputation: 590

Do not seed the random number generator more than once. Since your code probably runs all within the same second, every query to rand uses the same seed, so you'll get the same number every time.

Upvotes: 2

Related Questions