Generating 64-bit Hexadecial Numbers with Bash

In order to test my GPIO design whose width can vary from 1 to 64 bits I need to be able to generate random values to properly drive it’s data line. I need a method that’s fast and fairly random as it will run for many repetitions.

Before that I’ve used the verilog $random function and it behaves very good when you supply it with a random seed but it’s limit is 32 bits I think. I could have concatenated two of it’s outputs to create the 64 bit random number but I now need to generate it from bash script to create a memory dump with test vectors.

It was very promising to use the Date command  like this $date -%N . This way it gives out the date in nano seconds which is pretty random but the problem is that you can’t predict it’s length especially if you are generating random numbers in a loop you are bound to get values from this command of variable values and hence variable lengths when transferred to Hex. 

I tried to use this approach but it’s a loop that generates a hex character with each iteration so I would need to run it 16 times each time I am to generate a 64-bit number which is a lot of work since I am generating a lot of random numbers.

By this stage all that I need is to generate something random that’s bigger than 64 and I can change it to hex or binary using bc, a very useful command and I can manipulate it’s length as if it were a string using echo string manipulations and remove the extra bits.

So what how did I do it? Here is my pseudo code

  1. Generate a random number using $date -%N command.
  2. Change the number to binary then it’s number of bits.
  3. If the number of bits are less than the length you want, generate another random number using step one and concatenate the two strings. Repeat until step two evaluates true.
  4. eliminate any extra bits
  5. change the binary number to Hex

This approach generates my 64 bit Hex in just 3 iterations and saves me a lot of time.

 

You may also like...

1 Response

  1. jinahya says:

    any intention to share your code?

Leave a Reply