0

I was searching for a method for generating random numbers in C++. I came to know about rand() and srand(). Suddenly an idea tweaked in my mind of merging rand as a parameter of srand. i.e. srand(rand)

I just wanted to know how good is it compared to other methods of generating random numbers like using time as parameter of srand and other algorithms? Or if anyone else has some other cool tricks for generating random numbers please do comment...

Edit: I am using this a for generating a set of numbers in an array. A sample code goes like this:

int a[n];
for(int i=0; i<n; i++){
srand (rand());
a[i]=rand();
}
  • 3
    Did you consider to have a look at the c++ standard [random number generation library](https://en.cppreference.com/w/cpp/numeric/random)? – πάντα ῥεῖ Mar 09 '19 at 13:54
  • If you don't seed rand it will always give the same number – Mitch Mar 09 '19 at 13:54
  • 2
    Without seeding, `rand` gives you the same name first number every time you run the program, and seeding `srand` with the same number will give you the same sequence every time. To be even remotely 'random', you need to put something in that has a different value every time you run the program - like `time`. – Aganju Mar 09 '19 at 13:54
  • 1
    The workings of `srand()` and `rand()` are implementation-defined, but typically rather poor in comparison with alternatives. That's why (since C++11) the C++ standard specifies other ways of generating of generating pseudo-random values. – Peter Mar 09 '19 at 14:04
  • 2
    Without a prior `srand()`, `rand()` typically starts out with the same seed each time. Congratulations: you will now call `srand()` using the same seed value also, thereby accomplishing absolutely nothing useful, towards your original goal. – Sam Varshavchik Mar 09 '19 at 14:36
  • To make things clear I am using this for generating a set of numbers in array so after every element the value of rand changes and hence srand is seeded with a new number so every number is distinct – Aaket Chaurasia Mar 09 '19 at 14:36
  • Here's a sample code int a[n]; for(int i=0; i – Aaket Chaurasia Mar 09 '19 at 14:38
  • did you even try running it? [calling `srand` multiple times doesn't work](https://stackoverflow.com/q/7343833/995714). In your case the result of `rand()` is predictable so the seed is also the same every time. in C++ use [use `std::random_device` instead](https://stackoverflow.com/q/50662280/995714) – phuclv Mar 09 '19 at 14:49
  • @Peter -- a minor technicality: the workings of `srand()` and `rand()` are **implementation-specific**. Formally, they are not **implementation defined**. In the C++ standard, "implementation defined" means that the compiler is required to document what it does. – Pete Becker Mar 09 '19 at 14:55
  • Well, no, it's not a good idea. Consider looking at this talk https://www.youtube.com/watch?v=6DPkyvkMkk8 about ``, if you have some time. – Bob__ Mar 09 '19 at 15:09
  • Possible duplicate of [generate random number in c++](https://stackoverflow.com/questions/53773075/generate-random-number-in-c) or [Generate random numbers using C++11 random library](https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library) – Robert Andrzejuk Mar 09 '19 at 16:41

0 Answers0