Ah, sorry, Raul just pointed out to me that you'd already tried indexed read.  
Apologies for the noise.

Just so you know, 1!:1 and friends will always return literals (type 2).  This 
is similar to 'char' being (mostly) synonymous with 'byte' in C.  If you read a 
binary file with 1!:1, you'll still get a literal result. If the chars (bytes) 
are not printable ASCII characters, they may be invisible in the session 
manager.  In this case, you can distinguish null (empty) results from invisible 
results using #results, which will give you the length of the character array, 
in bytes.

If your results are not empty, you can then convert sequences of bytes to 
machine-native integers using 3!:4, or floats (doubles) using 3!:5, or their 
standard aliases ic and fc (respectively).

So, for example, if you try 

  chars =: 1!:11 '/dev/random';0 4 

and #chars returns 0, then you've got an empty result, and you'll have to find 
another method (maybe along the lines Raul sketched).  However, if #chars 
returns 4 (which should be the only other possible result) then on a 32-bit 
machine

   randInt =: ic chars 

Should convert it to an integer, which you can use as you like.  On a 64-bit 
machine, you'd have to instruct 1!:11 to  read 8 bytes.

-Dan


Please excuse typos; composed on a handheld device.

On Mar 13, 2013, at 7:45 AM, Dan Bron <[email protected]> wrote:

> Unqualified, 1!:1 reads the entire file, but /dev/random is an endless 
> stream.  Can you try indexed read? As in
> 
> 1!:11 '/dev/random';0 4 NB. Read 4 chars from file, starting at the beginning 
> 
> Note we're using one!:eleven, not one!:one .
> 
> Also, J includes its own PRNG, which you can use stand-alone, or to generate 
> a seed for your PRNG.  See
> 
> http://www.jsoftware.com/help/dictionary/d640.htm
> http://www.jsoftware.com/help/dictionary/d641.htm
> 
> -Dan
> 
> Please excuse typos; composed on a handheld device.
> 
> On Mar 13, 2013, at 4:48 AM, Jo van Schalkwyk <[email protected]> wrote:
> 
>> Hi All
>> 
>> I'd like to obtain a seed value (for a pseudorandom number generator) from
>> /dev/random, but when I try:
>> 
>> RAND =: '/dev/random'
>> read =: 1!:1
>> seed =: read < RAND
>> 
>> ... I seem to get a null string. What am I doing wrong? (I'm using Ubuntu
>> Linux v 12.10 and J701/2011-01-10/11:25).  In Perl (this works on my
>> machine), I'd say:
>> 
>> #!/usr/bin/perl
>> open(RAND, "/dev/random") or die "Can't open dev/random\n";
>> read(RAND, $seed, 4);
>> close(RAND);
>> print("Value is ", unpack("H*", $seed), "\n");
>> 
>> I've tried running J from the command line under sudo just in case there
>> was some 'security' issue, but this did nothing different. I'm not even
>> sure if this is something to do with formatting, or inability to access
>> /dev/random --- I suspect the former. When I say 3!:0 seed, I get type 2
>> (literal).  If I use e.g. rf =: 1!:21 < '/dev/random' I obtain a file
>> number, but I get the same null result on reading it, and an indexed read
>> fails as expected.  I can't find anything on /dev/random on the J website.
>> 
>> Regards Jo.
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to