开发者

Generate 12 Digit HEX number in KSH

I need to generat开发者_开发问答e 12 digit Hex numbers in KSH on Solaris

Thanks


#!/bin/ksh
set -A hex 0 1 2 3 4 5 6 7 8 9 A B C D E F
for i in {1..12}
do
   printf ${hex[$((RANDOM%16))]}
done


Start with this Python program, hex12.py.

hex12.py

#!/usr/bin/env python
import random
import hashlib
h= hashlib.sha1(str(random.random())).hexdigest()
print h[:12]

In your shell you can now use hex.py to create 12 hex digits on standard out.


Try this one:

DIGITS=`head -c 6 /dev/urandom | od -x | head -n 1 | sed -e 's/^0* //' -e 's/ //g'


As RANDOM variable generates a 15 bit number (from 0 to 32767) you can concatenate several RANDOM values.

You will need a 48 bit number as 12 hex digits are 12 * 4 = 48 bits.

Either:

$ printf '%x\n' $(( ((RANDOM<<15|RANDOM)<<15|RANDOM)<<3|RANDOM%8 ))
9142467b46d3

Or:

$ printf '%x' $((RANDOM%4096)) $((RANDOM%4096)) $((RANDOM%4096)) $((RANDOM%4096)); echo
808878c21e19
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜