Sujet : Re: lun - Lucky Number
De : rich (at) *nospam* example.invalid (Rich)
Groupes : sci.cryptDate : 09. Mar 2025, 05:19:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vqj4rk$ei2k$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Marcel Logen <
333200007110-0201@ybtra.de> wrote:
Rich in sci.crypt:
Ok, here's a simple random number generator using /dev/urandom:
>
#!/usr/bin/tclsh
>
set fd [open /dev/urandom {RDONLY BINARY}]
binary scan [read $fd 16] ww hi lo
close $fd
>
puts "Your 128-bit random number is: [expr {abs($hi)*2^64 + abs($lo)}]"
Interesting. But what is "ww"?
Partially an attempt to make an unspoken point. For those of us who
don't know go, reading go code can leave us /wondering/ what's going on
(at least until we dig into the go docs enough to figure it out).
The real explanation is it is the Tcl binary command operator for
"convert 8 bytes into a signed 64-bit integer". In which case I
suspect "w" was chosen to be short for (w)ide.
The two w's in a row convert consequitive 8 bytes into unsigned
64-bit integers, storing the first into variable "hi" and the second
into variable lo.
Then the [expr] call converts hi and lo into a single longer integer
(which, thinking about it now, since I have to abs() them, means this
is really outputting a 126 bit value instead.
16 * 8 = 128 -> OK, but ...:
Four sample runs:
>
~$ ./random.tcl
Your 128-bit random number is: 12845925169244013330
~$ ./random.tcl
Your 128-bit random number is: 12267131317558982811
~$ ./random.tcl
Your 128-bit random number is: 10434877321040400260
~$ ./random.tcl
Your 128-bit random number is: 16618556391581443091
^^^
64 (?)
| $ echo 'l(16618556391581443091)/l(2)' | bc -l
| 63.84942886744934185453
But I'm not entirely sure if I'm on the right track.
Ugh..., no, you are right. ^ is xor. ** is power. The code should be:
#!/usr/bin/tclsh
set fd [open /dev/urandom {RDONLY BINARY}]
binary scan [read $fd 16] ww hi lo
close $fd
puts "Your 126-bit random number is: [expr {abs($hi)*2**64 + abs($lo)}]"
~$ ./random.tcl
Your 126-bit random number is: 169336852709816669373259866667412280949
~$ ./random.tcl
Your 126-bit random number is: 32633310813732482344192030424429288689
~$ ./random.tcl
Your 126-bit random number is: 76929973179533891725985594329257253768
~$ ./random.tcl
Your 126-bit random number is: 49046152244045834428660372315683579977
~$ echo 'l(49046152244045834428660372315683579977)/l(2)' | bc -l
125.20547946978097462900
or
~$ echo 'l(169336852709816669373259866667412280949)/l(2)' | bc -l
126.99316358625335794390