9

I wrote this C code below, when I loop, it returns a random number. How can I achieve the 5 different random values if myrand() is executed?

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

int myrand() {
    int ue_imsi;
    int seed = time(NULL);
    srand(seed);
    ue_imsi = rand();

    return ue_imsi;
}

int main()
{
    int value = 0;
    int i=0;
    for (i=0; i< 5; i++)
    {
        value =myrand();
        printf("value is %d\n", value);
    }
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
xambo
  • 399
  • 2
  • 4
  • 17
  • 10
    Seed **once** in your life, not all the time. – Kerrek SB Nov 29 '11 at 16:43
  • You're initializing the random number generator with the same number over and over again (as long as `time(NULL)` returns the same value). Don't do that if you want the numbers to look like random. Also remember to `#include ` for `time()` prototype. – pmg Nov 29 '11 at 16:45
  • 1
    possible duplicate of [Always repeated numbers given by rand()](http://stackoverflow.com/questions/4859089/always-repeated-numbers-given-by-rand). Please use the search facilities of SO before asking a question. – Jens Gustedt Nov 29 '11 at 19:53

5 Answers5

25

Seeding the generator should be done once(for each sequence of random numbers you want to generate of course!):

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

int main()
{
    int seed = time(NULL);
    srand(seed);
    int value = 0;
    int i=0;
    for (i=0; i< 5; i++)
    {
        value =rand();
        printf("value is %d\n", value);
    }
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
5

Move the srand() call into main(), before the loop.

In other words, call srand() once and then call rand() repeatedly, without any further calls to srand():

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

int main()
{
    int value = 0;
    int i = 0;
    srand(time(NULL));
    for (i = 0; i < 5; i++)
    {
        value = rand();
        printf("value is %d\n", value);
    }
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    @xambo: When you call `time()` several times in quick succession, it is likely to return the same value, since the clock hasn't ticked. When you call `srand()` with the same seed, the following call to `rand()` will return the same value. As I said, don't try to reinitialize the generator every time you need a random value. – NPE Nov 29 '11 at 16:47
0

The point of seed() is to start the sequence of random numbers with a known value,
you will then always get the same sequence of numbers given the same seed.

This is why you have seed(), it both allows you to generate the same sequence for testing, or given a random seed (typically the time) you get a different sequence each time

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
0

Try this:

#include <stdio.h>
#include <time.h>
int main(void) {
    for (int i = 0; i < 10; i++) {
        printf("%ld\n", (long)time(NULL));
    }
}

My "guess" is that 10 equal values will be printed :)

pmg
  • 106,608
  • 13
  • 126
  • 198
-2

If you want to reseed (for extra randomness) every time you call random(), here's one way you could do that:

srandom( time(0)+clock()+random() );
  • time() updates once per second, but will be different every time you run your program
  • clock() updates much more frequently, but starts at 0 every time you run your program
  • random() makes sure that you (usually) don't reseed with the same value twice in a row if your loop is faster than the granularity of clock()

Of course you could do more if you really, really, want randomness -- but this is a start.

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • Oops -- using `clock()` doesn't add much value here since it is roughly deterministic if your program doesn't have any I/O delays. – Brent Bradburn Oct 18 '14 at 02:41
  • Well unless you somehow consistently get 0, clock will add at least some extra bits of entropy, so it matters. – this Jun 13 '15 at 18:55
  • http://scruss.com/blog/2008/10/12/pgmrnoise-a-more-random-or-less-repeatable-pgmnoise/ – Brent Bradburn Mar 22 '16 at 14:15
  • `getpid()` would probably be a good addition to resolve the limitations of `time()`. – Brent Bradburn Feb 07 '17 at 16:32
  • https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – Brent Bradburn Jun 10 '17 at 20:54
  • Reseeding for 'extra randomness' is like opening and closing a door several times for 'extra closedness' – Anti Earth Mar 31 '18 at 16:41
  • @AntiEarth: A pseudo-random number generator produces a *specific* sequence if called repeatedly after seeding. That makes it hackable. Reseeding for every call can increase randomness. For most purposes, it probably doesn't matter, but the fact remains. – Brent Bradburn Apr 01 '18 at 02:23
  • Rolling your own security solution is a no-no. I've presented this here as a concept for learning (based on what was being done in the question) -- not as a recommendation for serious use. – Brent Bradburn Apr 01 '18 at 02:27
  • @nobar: And reseeding a psuedo-random number generator with the same sequence (psuedo-random or not) of seeds produces a *specific* sequence. Your 'extra randomness' argument takes for granted a uniform distribution of the *first* output of a generator; this sometimes isn't even true for uniformly random seeds, but here you seed with a strictly increasing large value, and some generators produce barely differing *sequences* when the seed is large! – Anti Earth Apr 01 '18 at 18:53