Page 1 of 1

Access the hwrng.bin file (random number generation)

Posted: Fri May 31, 2019 7:31 am
by RuudB
Hello all!

I do have a little problem which I can't seem to solve!
I did read a lot of information online but can't find a solution.
Maybe one of you can help me?

For a project at my work I need a lot of random number input. Its easy enough to find online RNG's but hey! I have a Rpi 3b+ so i like to use my own for random numbers. 8-)

With online info i activated the Hwrng and with:

Code: Select all

 dd if=/dev/hwrng of=hwrng-data.bin bs=1024 count=1024
I can make a 1mb file filed with random numbers...

But i have no clue how to see those random numbers on my screen.. If i open the file i just see a bunch of unknown symbols.
I also tried to print True random numbers on my screen but it seems not to work for me.

Anyone know how to properly open the hwrng.bin fil? or how i can print a bunch of number on my screen?

If i have a working code to print on my screen that would be awesome too! Than I can print them on my 16x2 LCD and keep them rolling or refreshing.


Thank you in advance!

Re: Access the hwrng.bin file (random number generation)

Posted: Fri May 31, 2019 8:48 am
by jamesh
You can convert binary files to hex numbers using hexdump. This 'cat's the urandom devices through hexdump to make it human readable.

Code: Select all

cat /dev/urandom | hexdump
Or in your case

Code: Select all

hexdump hwrng-data.bin

Re: Access the hwrng.bin file (random number generation)

Posted: Fri May 31, 2019 3:10 pm
by pfletch101
jamesh wrote:
Fri May 31, 2019 8:48 am
You can convert binary files to hex numbers using hexdump. This 'cat's the urandom devices through hexdump to make it human readable.

Code: Select all

cat /dev/urandom | hexdump
Or in your case

Code: Select all

hexdump hwrng-data.bin
This treats the binary file or the output of the urandom device as a byte stream (which is basically what it is - actually a bit stream). I think that the OP may be looking for a sequence of discrete random numbers - though he needs to specify the range in which these should lie.

Re: Access the hwrng.bin file (random number generation)

Posted: Sat Jun 01, 2019 2:54 am
by RuudB
Thank you so much!
It did cost me so much time but its finally starting to work out haha! So simple but you have to know ..

pfletch101 wrote:
Fri May 31, 2019 3:10 pm
This treats the binary file or the output of the urandom device as a byte stream (which is basically what it is - actually a bit stream). I think that the OP may be looking for a sequence of discrete random numbers - though he needs to specify the range in which these should lie.
Yes, that is right, preferable I need random numbers in the range 0 to 9
If that is possible that would really be what I need!

So once more I will polity ask for your help!
Thanks in advance! And have a great weekend!

Re: Access the hwrng.bin file (random number generation)

Posted: Sat Jun 01, 2019 9:00 pm
by pfletch101
RuudB wrote:
Sat Jun 01, 2019 2:54 am
Thank you so much!
It did cost me so much time but its finally starting to work out haha! So simple but you have to know ..

pfletch101 wrote:
Fri May 31, 2019 3:10 pm
This treats the binary file or the output of the urandom device as a byte stream (which is basically what it is - actually a bit stream). I think that the OP may be looking for a sequence of discrete random numbers - though he needs to specify the range in which these should lie.
Yes, that is right, preferable I need random numbers in the range 0 to 9
If that is possible that would really be what I need!

So once more I will polity ask for your help!
Thanks in advance! And have a great weekend!
Here is (Python 3) code to print sequential random single-digit numbers:

Code: Select all

from time import sleep
f=open('/dev/urandom','rb')
while True:
    rndbytes=f.read(1)
    rndint=rndbytes[0]
    if rndint < 10: # ignore numbers above 9 - should not disturb randomness of remainder
        print (rndint) # this can be replaced by code which uses the random number
        sleep(1)

Here, probably more usefully :) is a function to return a random number between 0 and 9.

Code: Select all

def randint09():
    with open('/dev/urandom','rb') as f:
        while True:
            rndbytes=f.read(1)
            rndint=rndbytes[0] & 15 # mask off high bits (added in edit)
            if rndint < 10: # ignore numbers above 9 - should not disturb randomness of remainder
                return rndint

# the following code uses the function to generate random integers to print to the screen
while True:
    print(randint09())
    sleep(1) # assumed to have been imported
 
It is better to use the urandom device directly than to generate a file and use that, since, in the latter case, you will always see the same sequence. The function could easily be generalized to generate numbers in any specified range - for ranges extending beyond 255, bytes would be read and combined two (or more) at a time to get the initial random integer - then you can throw away anything outside the desired range.

{EDIT} The commented change in the randint09 function should not change the validity of the code but it will make it quite a bit faster/more efficient, since only (on average) 6 out of 16 of the original random bytes will be discarded, needing at least one additional 'try', while without the masking step, many more 'tries' to get a 'hit' will be necessary for some original sequences. Obviously, different integer ranges for the random output would require the use of appropriate masks.

Re: Access the hwrng.bin file (random number generation)

Posted: Mon Jun 03, 2019 4:18 am
by RuudB
Hi!

Thank you so much for your effort! :)

It costed me some time but I got it running in Idle 3 !! (I'm a newbie... i tried to open the .py file in the terminal :roll: ...)
The code and extra info are very helpful! I need to read in it a bit more to fully understand the difference between the first and second code, but it is working and i,m very happy!

Best regards,
Ruud