There was an upload recently on generating a pseudo-random file when /dev/[u]random does NOT exist.
This does not need /dev/random, /dev/urandom or $RANDOM either...
(I assume $RANDOM relies on the /dev/[u]random device in some way.)
This code uses hexdump just because I like hexdump for ease of use, but od or xxd could well be used with some minor code alteration instead...
The random file is NOT great, and it IS SSLLOOWW, but hey - IT WORKS...
I found this problem interesting, although it may not be of much use...
Code:
#!/bin/bash --posix
# random.sh
> /tmp/binary.file
count=1
random_file_size=1024
hex_value="00"
dec_value=0
seed=17
subscript=9999
file_size=$(echo -n $(wc -c < /bin/bash))
# This could produce an error if inputted value is not a number!
read -p "Enter a pseudo-seed from 9999 to 32767:- " -e subscript
if [ "$subscript" == "" ]; then subscript=9999; fi
if [ $subscript -le 9999 ]; then subscript=9999; fi
if [ $subscript -ge 32767 ]; then subscript=32767; fi
# This is the DEMO working loop and in this case generates a 256 byte "random" file.
for count in $( seq 1 1 $random_file_size )
do
hex_value=$(hexdump -n1 -s$subscript -v -e '1/1 "%02x"' /bin/bash)
dec_value=$(hexdump -n1 -s$subscript -v -e '1/1 "%u"' /bin/bash)
subscript=$[ ( $subscript + $dec_value + $seed ) ]
if [ $subscript -ge $file_size ]; then subscript=9999; fi
echo -e -n '\x'"$hex_value" >> /tmp/binary.file
done
hexdump -C /tmp/binary.file
With "not great" you probably mean that the periodicity is low: there are pseudo-seeds where the periodicity is less than 30.
The quality of the output of the algorithm is also very sensitive to the value of seed (line 10).
The quality of the output of the algorithm is also very sensitive to the value of seed (line 10).
I was well aware of this that is why I used a variable instead of a fixed value inside line 25.
The idea was to use the last byte read as the next "seed" but I didn't pursue it any further. "seed" must always
have a value greater than or equal to 1 and less than or equal to decimal value 256 added to the current byte in use.
(I assumed you guys would see the flaw and correct as you need.)
However if the current byte value would be zero then "seed" would have to be a minimum value of "1".
(This is taken care of with the limitation of fixing "seed" in the script shown.)
Not because of anything to do with binary zero in shell scripting per-se but it would just stop there until the
loop finished and give that character value at that point continuously ( <- if that sentence is lucid enough. ).
Bazza...
EDIT: Made more lucid.
This is a simple way of making it a little better without the complexity of storing the previous and current
bytes read. (Line 26.)
Code:
if [ $subscript -ge $file_size ]; then subscript=$[ ( $dec_value + 9999 ) ]; fi
Last edited by wisecracker; 09-21-2013 at 06:27 PM..
Reason: Rewrite to make more lucid...
I am using the below to random generate a password but I need to have 2 numeric characters and 6 alphabetic chars
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 8 ; echo ''
6USUvqRB
------ Post updated at 04:43 PM ------
Any Help folks - Can the output be passed onto a sed command to... (9 Replies)
Dear UNIX Friends,
I was wondering if there is a random RGB color generator or any function in any unix platforms.
Please share your ideas.
Thanks (2 Replies)
Hello
I created 3 files by:
dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1
dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2
dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3
Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
A very simple crude sinewave generator.
The file required is generated inside the code, is linear interpolated and requires /dev/audio to work. Ensure you have this device, if not the download oss-compat from your OS's repository...
It lasts for about 8 seconds before exiting and saves a... (5 Replies)
I want to develop a script of the following form:
#!/bin/bash
# Function 'listen' opens a data stream
# which stores all incoming bytes in
# a buffer, preparing them to be
# grabbed by a following function
# which appears at random
# intervals during the execution of
# the script
... (11 Replies)
Hi,
I need a script that will generate a set of random strings in sequence, with the ability to predetermine the length, quantity, and alphabet of individual string, and to use the outputs of earlier strings in the sequence to define the parameters of later strings. For examples, I might want... (5 Replies)
Hi,
I need to create a table with random sentences. I need lines that are upto 1000 characters in lenght. I need a random sentence generator that will create sentences and output it to a text file. The sentences should be of lenght varying from 1 to 1000.
Does anyone know how this can be... (7 Replies)
In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server.
For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match.
Please... (4 Replies)
I am trying to find a way to generate random numbers within a shell script.
Does Solaris have a utility that will generate random numbers?
Thanks in advance.
B (3 Replies)