0

I know how to generate random nos by using clock function of time.h, is there any other way of doing this without using clock of system

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Prince
  • 33
  • 5
  • Are you talking about using the clock for `srand()`? – Austin Brunkhorst Nov 16 '18 at 18:30
  • There is an entire header for this: [``](https://en.cppreference.com/w/cpp/numeric/random) – Mgetz Nov 16 '18 at 19:10
  • Possible duplicate of [Random number generation in C++11: how to generate, how does it work?](https://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-does-it-work) – Mgetz Nov 16 '18 at 19:11
  • @MGetz I disagree with closing as duplicate since this question does permit for some discussion about alternate sources of entropy than the current system time, and not just how to use C++'s `` library instead of C functions – Govind Parmar Nov 16 '18 at 20:32

1 Answers1

2

This is a difficult problem in computing since computers are fundamentally deterministic machines and cannot generate random numbers without an external source of entropy. Seeding a pseudorandom number generator to the current system time is sufficient to emulate "true randomness" for many purposes.

If you want to eschew seeding to the current system time and still generate unique numbers from a PRNG, here are some options (note that I am assuming you aren't talking about a PRNG that is intended to be cryptographically secure, which has many other requirements):

  1. If your machine has ASLR enabled, get the current value of the stack pointer register
  2. The number of bytes available on the current storage device or the current user's remaining disk quota
  3. A hash of the most recently sent or received network packet from the machine

That said, most of this is probably overkill. Why not use the current system time as a seed? That's the way most developers do it until they need something truly unpredictable, like a CSPRNG.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85