Deutsch   English   Français   Italiano  
<vqj4rk$ei2k$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Rich <rich@example.invalid>
Newsgroups: sci.crypt
Subject: Re: lun - Lucky Number
Date: Sun, 9 Mar 2025 04:19:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <vqj4rk$ei2k$2@dont-email.me>
References: <vqg3sj$3q7rg$1@stefan.eternal-september.org> <vqi2io$8r9n$1@dont-email.me> <vqi2qe$90e8$1@stefan.eternal-september.org> <vqidud$arf3$2@dont-email.me> <20250308sa223433@o15.ybtra.de>
Injection-Date: Sun, 09 Mar 2025 05:19:01 +0100 (CET)
Injection-Info: dont-email.me; posting-host="5aa0a10a67ed880b9a8bff36834f0523";
	logging-data="477268"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18kacyZ9357eEldHodj8Qwd"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:pRfhaas8J1T9l7d3lr4CIkYYUMw=
Bytes: 3613

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