How to use function srand() with time.h? [duplicate]
Asked Answered
C

5

13

My program contains code that should generate a random positive integer number every time I execute it. It generates random numbers but only once. After that, when I execute same code, it gives me same values, and it is making my code useless.

I started with the rand function, and then I used the srand() function with the time.h header file, but still it is not working properly.

#define size 10
for(i=0;i<size;i++)
    Arr[i] = rand()%size;

First call (random):

6 0 2 0 6 7 5 5 8 6

Second call (random but same as previous):

6 0 2 0 6 7 5 5 8 6

Later I visited Stack Overflow questions and I read about the srand() function, and I used it as:

#include<time.h>
for(i=0;i<size;i++)
    Arr[i] = srand(time(NULL));

First call:

-10327 -10327 -10327 -10327 -10327 -10327 -10327 -10327 -10327 -10327

Second call:

-10326 -10326 -10326 -10326 -10326 -10326 -10326 -10326 -10326 -10326

It is giving me different (but not random values). I've defined Arr[i] as unsigned int, and still I am getting negative values.

Centurial answered 15/5, 2013 at 15:31 Comment(7)
did you set your seed? Set your seed with the time function.Semite
You only need to call srand once to seed the rand generator. Don't call srand each time to get your numbersSpragens
You probably ought to read the manpage, which explains how these functions work, and provides a working example.Quietude
@Magn3s1um I don't know what you are talking aboutCenturial
You set your seed with the current time: srand(time(null)). Then you call rand. That way you get a unique seed every single time. The seed changes the random algorithm. Do you understand now?Semite
is there any specific reason for using srand(), if not may be you can follow [How to use /dev/random or urandom in C? stackoverflow ](#2572866)Welladvised
@Magn3s1um: Clear and concise explanation :)Demonolater
C
47

You need to call srand() once, to randomize the seed, and then call rand() in your loop:

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

#define size 10

srand(time(NULL)); // randomize seed

for(i=0;i<size;i++)
    Arr[i] = rand()%size;
Chape answered 15/5, 2013 at 15:35 Comment(1)
srand() takes an unsigned int as an input, so time(NULL) should be recast.Kort
B
4

Try to call randomize() before rand() to initialize random generator.

(look at: srand() — why call it only once?)

Belgium answered 15/5, 2013 at 15:35 Comment(7)
Please write me the code, it would be really helpful if you put itCenturial
randomize is not a standard function - perhaps you mean srand ?Chape
Paul R, yes, i mean srand() from the "stdlib.h". Thank you for adding comment. My mistake.Belgium
cplusplus.com/reference/cstdlib/srand has an example where srand is called more than once.Belgium
That example is to illustrate that not calling srand() has the same effect as calling srand(1). It should be made clearer, though.Fraley
@Ðeepak, may this information will help you: https://mcmap.net/q/64620/-srand-why-call-it-only-onceBelgium
@Daniel Fischer, thank you for the hint.Belgium
F
3

If you chose to srand, it is a good idea to then call rand() at least once before you use it, because it is a kind of horrible primitive psuedo-random generator. See Stack Overflow question Why does rand() % 7 always return 0?.

srand(time(NULL));
rand();
//Now use rand()

If available, either random or arc4rand would be better.

Fabron answered 15/5, 2013 at 16:5 Comment(0)
F
-2
#include"stdio.h"
#include"conio.h"
#include"time.h"

void main()
{
  time_t t;
  int i;
  srand(time(&t));

  for(i=1;i<=10;i++)
    printf("%c\t",rand()%10);
  getch();
}
Father answered 19/9, 2014 at 7:58 Comment(0)
F
-2
#include"stdio.h"//rmv coding for randam number access

#include"conio.h"

#include"time.h"

void main()
{
    time_t t;
    int rmvivek;

    srand(time(&t));
    rmvivek=1;

    while(rmvivek<=5)
    {
        printf("%c\t",rand()%10);
        rmvivek++;
    }
    getch();
}
Father answered 19/9, 2014 at 8:1 Comment(1)
this method encryption and decryption progamming used subpartFather

© 2022 - 2024 — McMap. All rights reserved.