Generate Random numbers without using any external functions

后端 未结 9 979
梦如初夏
梦如初夏 2020-12-08 16:26

This was questions asked in one of the interviews that I recently attended.

As far as I know a random number between two numbers can be generated as follows

9条回答
  •  一生所求
    2020-12-08 16:52

    Here I am suggesting some sources with comment may be you find helpful:

    • System Time : Monotonic in a day poor random. Fast, Easy.
    • Mouse Point : Random But not useful on standalone system.
    • Raw Socket/ Local Network (Packet 's info-part ) : Good Random Technical and time consuming - Possible to model a attack mode to reduce randomness.
    • Some input text with permutation : Fast, Common way and good too (in my opinion).
    • Timing of the Interrupt due to keyboard, disk-drive and other events: Common way – error prone if not used carefully.
    • Another approach is to feed an analog noise signal : example like temp.
    • /proc file data: On Linux system. I feel you should use this.

      /proc/sys/kernel/random: This directory contains various parameters controlling the operation of the file /dev/random.

      The character special files /dev/random and /dev/urandom (present since Linux 1.3.30) provide an interface to the kernel's random number generator.

      try this commads:

      $cat /dev/urandom   
      

      and

      $cat /dev/random
      

      You can write a file read function that read from this file.

      Read (also suggests): Is a rand from /dev/urandom secure for a login key?

    `

提交回复
热议问题