-1

Please check code given below. The random number it generates for each execution is the increment of the previous generated number in the previous execution.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand(time(NULL));

    cout<<"\n Random Number : "<<rand()<<endl;

    cin.get();
    return 1;
}

Please executeit for 5-6 times and you will see that the random numbers are increasing for each execution and they are very close to each other.

Note : Please use CodeBlocks or Visual studio to check it, not the online compilers.

bashburak
  • 105
  • 1
  • 5
  • 4
    There are almost no guaranties on the randomness of `rand()`. Your behavior may be due to the fact that `srand` is seeded with slightly larger values (the current time) each time you test. It might just be returning the seed on the first call each time, or something closely related to it. – François Andrieux Mar 27 '19 at 14:25
  • you say "the next generated random number is always bigger" but you only look at `rand()%500`, hence you dont really know if the next number is bigger or smaller. `rand()` is known to be of poor quality and naively taking `%` is one way to observe that – 463035818_is_not_an_ai Mar 27 '19 at 14:30
  • 2
    `time(NULL)` gives you the time in seconds as an integer, so if you call the program twice in the same second, you will get the same number twice. If you call it in the next second, you will get completely different numbers: https://ideone.com/McE7CD – mch Mar 27 '19 at 14:30
  • 3
    moreover you seem to compare random numbers where each is generated with a different seed, where the seed have a strong relation. To get anything meaning full you would have to look at lots of random numbers generated from the same seed – 463035818_is_not_an_ai Mar 27 '19 at 14:31
  • 2
    Duplicate of https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c ? – William D. Irons Mar 27 '19 at 14:34
  • I think my question have nothing to do with ill-formed main definiton @eerorika – bashburak Mar 28 '19 at 06:18
  • @mch As I already mentioned above, "When I put `x = rand()%500; part in a loop ... it works properly". But the problem arises from execution of the program I guess. – bashburak Mar 28 '19 at 06:24
  • @bashburak did you see the `+ i` in `srand(time(NULL) + i);` in my example? It will simulate 100 program calls each with a 1 second gap. – mch Mar 28 '19 at 07:43
  • @mch it also works if you do not put +i in srand(rime(NULL)). It still works properly when you do with loops. Because execution happens one time only. Check here : [link](https://ideone.com/Tkha11) – bashburak Mar 28 '19 at 07:56
  • 2
    Possible duplicate of [How to generate a random number in C++?](https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c) – flau Mar 28 '19 at 11:36

1 Answers1

-1

Actually I found a way to solve my problem but still it might not be an answer to my question. Anyway the problem is not about srand() or rand() functions but it is about the function time(NULL). Since I am trying to run this code on Windows, instead of using time(NULL) as a parameter for srand(), I used GetTickCount() and now it generates random numbers properly for each execution.

#include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

int main()
{
    srand(GetTickCount());

    cout<<"\n Random Number : "<<rand();

    cout<<"\n";
    cin.get();
    return 1;
}
bashburak
  • 105
  • 1
  • 5