A crude random byte generator...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers A crude random byte generator...
# 1  
Old 08-24-2013
A crude random byte generator...

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

And the on screen results...
Code:
Last login: Sat Aug 24 21:32:53 on ttys000
AMIGA:barrywalker~> ./random.sh
Enter a pseudo-seed from 9999 to 32767:- 12345
00000000  48 48 80 f5 6b ff fe fa  11 c7 00 4e bf 05 3a c0  |HH..k......N..:.|
00000010  00 0f 8b 3c 48 c7 00 e8  35 84 00 00 00 00 00 3d  |...<H...5......=|
00000020  00 00 f6 83 63 e8 00 15  44 ff 0a eb c7 c7 10 a3  |....c...D.......|
00000030  04 07 0d 3f 05 0a eb 00  30 0a 00 cb 03 11 00 eb  |...?....0.......|
00000040  00 f7 c0 55 1c 89 00 00  89 5b e5 c7 40 53 78 ab  |...U.....[..@Sx.|
00000050  00 83 c4 00 22 d8 56 89  f0 cd 48 0f b8 80 c3 4c  |....".V...H....L|
00000060  f8 55 53 8d 18 49 48 43  48 45 00 49 08 48 5d 64  |.US..IHCHE.I.H]d|
00000070  41 31 c3 c0 48 4d 54 0f  48 ff 75 7d 00 72 48 cd  |A1..HMT.H.u}.rH.|
00000080  4d 8b ff 5d e8 01 05 ff  ff 89 24 83 c2 b1 41 06  |M..]......$...A.|
00000090  ff 07 8b 85 4c 53 43 0e  0d 74 ff 48 41 89 5e 89  |....LSC..t.HA.^.|
000000a0  05 f2 75 0f 00 48 e5 4c  48 48 00 00 00 e8 74 eb  |..u..H.LHH....t.|
000000b0  89 85 8b 0f 38 00 00 3c  76 f6 22 74 c6 83 75 ff  |....8..<v."t..u.|
000000c0  c3 75 e8 00 00 48 00 5b  00 64 48 de c4 06 e8 06  |.u...H.[.dH.....|
000000d0  00 74 51 48 ff 05 f8 09  89 f6 01 9e 06 00 77 f4  |.tQH..........w.|
000000e0  11 85 e9 8d 29 eb eb ef  ff 00 00 48 d9 3d 8d d7  |....)......H.=..|
000000f0  c3 4c c7 c6 0d 8d 00 fa  c0 15 ff 41 df 35 00 c3  |.L.........A.5..|
00000100  33 49 21 75 89 06 5e ff  ce 89 00 09 00 48 27 63  |3I!u..^......H'c|
00000110  35 c7 bf 00 06 41 00 8c  c7 48 00 24 89 48 41 41  |5....A...H.$.HAA|
00000120  41 00 89 85 48 41 31 05  00 e8 ff bf ff 22 c0 05  |A...HA1......"..|
00000130  9a ba 8d ff 35 00 ef 00  ef f8 00 3e ac 8a 00 45  |....5......>...E|
00000140  5c 89 48 48 85 75 e8 22  17 00 01 c1 ff 0f 46 02  |\.HH.u."......F.|
00000150  89 59 48 75 ff 75 63 06  c4 00 63 3d 89 03 08 48  |.YHu.uc...c=...H|
00000160  85 89 89 80 8b ff 48 ff  1c bc 0f ff 45 48 ff ff  |......H.....EH..|
00000170  74 00 89 f9 c7 e9 75 41  ff f8 00 8d 41 c6 3c 00  |t.....uA....A.<.|
00000180  c6 ff 16 10 48 c0 48 49  5e 04 40 f6 00 85 0f 03  |....H.HI^.@.....|
00000190  fd 00 c0 83 09 64 00 00  53 8b 01 c6 00 55 45 5c  |.....d..S....UE\|
000001a0  4c 40 10 89 48 85 a2 48  aa db 83 48 00 ff 48 ac  |L@..H..H...H..H.|
000001b0  48 41 48 df 74 ec 00 ff  1f cc 00 22 49 30 ec 00  |HAH.t......"I0..|
000001c0  48 08 e8 48 f7 ed 04 fd  48 8d 54 00 4c 49 4c 89  |H..H....H.T.LIL.|
000001d0  48 48 c0 74 00 48 8d f7  06 fb 05 05 8d 49 14 c8  |HH.t.H.......I..|
000001e0  89 fe ff 00 04 d5 ff 01  00 4c 45 03 8b 00 75 45  |.........LE...uE|
000001f0  83 67 00 89 ff 62 7f ff  00 08 4c 8d ff 8d e7 e7  |.g...b....L.....|
00000200  d9 85 e7 ff 7c 00 85 3e  00 8b 31 ff 55 be c3 fd  |....|..>..1.U...|
00000210  c0 85 01 00 05 00 09 e8  5d 57 49 00 48 13 89 f2  |........]WI.H...|
00000220  05 48 83 85 eb 4b 00 ea  00 48 89 03 00 74 e8 00  |.H...K...H...t..|
00000230  00 89 e7 00 89 00 c0 5b  48 03 db 00 05 48 0c c7  |.......[H....H..|
00000240  00 ee 41 c7 00 fb ff eb  b2 8b 00 48 06 5b e7 0a  |..A........H.[..|
00000250  03 4c 00 83 1b 80 ac 83  83 18 c1 c0 ff 00 8d 41  |.L.............A|
00000260  8b fe 3b 44 e8 48 e8 83  e8 8d ff ad b2 7d d0 74  |..;D.H.......}.t|
00000270  26 74 f8 82 0b 29 ff ec  00 89 c6 8b 49 45 74 5d  |&t...)......IEt]|
00000280  76 00 1d 00 d1 48 00 dd  8b 48 30 63 41 3f 09 ba  |v....H...H0cA?..|
00000290  05 38 00 1e c6 95 00 08  00 85 25 1f 85 09 00 48  |.8........%....H|
000002a0  ff 8a 19 e8 45 8b 03 00  05 00 3d e8 e6 75 54 4c  |....E.....=..uTL|
000002b0  25 3d 78 55 41 ca de 8d  41 2b 0e 09 74 74 ff fd  |%=xUA...A+..tt..|
000002c0  00 e8 00 3b 00 00 0a 88  8d 18 ff c0 fe 88 74 04  |...;..........t.|
000002d0  00 00 fe 8d 85 ff e9 09  0f ff 83 48 4c 91 c0 b5  |...........HL...|
000002e0  de 06 c3 c0 08 0a 16 51  45 8d af 89 83 c1 00 c0  |.......QE.......|
000002f0  fb c0 89 89 00 74 40 ff  24 2d 74 00 c4 48 4c 48  |.....t@.$-t..HLH|
00000300  d8 01 08 05 e0 8b 45 c8  89 ed 8d 94 c6 48 41 48  |......E......HAH|
00000310  04 00 f3 00 55 18 06 e8  5d 01 e9 a4 00 00 20 7c  |....U...]..... ||
00000320  46 f6 ff 00 00 53 38 60  83 23 f8 00 c1 ff 02 89  |F....S8`.#......|
00000330  01 c0 be 4c 00 48 1c 48  89 40 45 55 e8 00 07 88  |...L.H.H.@EU....|
00000340  4c 34 05 73 08 08 ca 63  44 e9 00 63 f6 57 45 f6  |L4.s...cD..c.WE.|
00000350  eb e8 82 54 01 25 ff fb  78 53 41 f7 24 10 18 eb  |...T.%..xSA.$...|
00000360  e8 e8 7e 85 00 00 ff 48  a3 48 5d 90 45 41 3b 3f  |..~....H.H].EA;?|
00000370  ff 48 fe 45 ec ff bf 05  48 ff 45 05 0a 48 8d fd  |.H.E....H.E..H..|
00000380  39 53 1c 97 08 8b 86 88  63 48 74 00 9c df b8 85  |9S......cHt.....|
00000390  9c 00 48 44 bf 3d be 8d  00 85 8a 05 78 48 e8 be  |..HD.=......xH..|
000003a0  c9 e8 48 89 af 08 48 ff  52 48 48 70 ff bd 02 1f  |..H...H.RHHp....|
000003b0  d6 77 08 00 c7 04 0e 00  2b 00 b5 05 89 fc 89 00  |.w......+.......|
000003c0  fc 02 45 29 ff 8b 00 00  fc 01 00 ff 4c 8b 00 00  |..E)........L...|
000003d0  ac 04 00 3d 10 8b 05 75  e8 ff 79 89 e8 bd 00 7d  |...=...u..y....}|
000003e0  08 ff 8d fe 19 00 41 8d  00 48 8d 85 08 0f ff 06  |......A..H......|
000003f0  74 85 fb 83 41 89 8d fd  00 85 cf ff ff 8b 74 00  |t...A.........t.|
00000400
AMIGA:barrywalker~>

# 2  
Old 09-21-2013
Quote:
Originally Posted by wisecracker
The random file is NOT great
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).
# 3  
Old 09-21-2013
Hi figaro...
Quote:
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...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Random Password generator with 2 digits and 6 characters

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)
Discussion started by: infernalhell
9 Replies

2. Shell Programming and Scripting

awk (or) UNIX random RGB colors generator?

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)
Discussion started by: jacobs.smith
2 Replies

3. UNIX for Dummies Questions & Answers

Changing a special line and Byte in a random file

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)
Discussion started by: frhling
2 Replies

4. Shell Programming and Scripting

A Crude 1KHz Audio Sinewave Generator Demo...

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)
Discussion started by: wisecracker
5 Replies

5. Shell Programming and Scripting

Grab exactly one byte from a FIFO, at random intervals

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)
Discussion started by: vomv1988
11 Replies

6. Shell Programming and Scripting

Need specialized random string generator script

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)
Discussion started by: vajrajames
5 Replies

7. Shell Programming and Scripting

Random Sentence Generator

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)
Discussion started by: kaushys
7 Replies

8. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

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)
Discussion started by: krishmaths
4 Replies

9. Solaris

Does Solaris have a random number generator?

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)
Discussion started by: one_ring99
3 Replies
Login or Register to Ask a Question