Random number generator (device): Difference between revisions

Content added Content deleted
(Add NetRexx implementation)
Line 209: Line 209:
}</lang>
}</lang>


=={{header|NetRexx}}==
{{Works with|Mac OS X}} and probably other UNIX systems that provide <tt>/dev/random</tt> or <tt>/dev/urandom</tt> random data source devices.<br />
<lang NetRexx>/* NetRexx */
options replace format comments java crossref savelog symbols binary

import java.math.BigInteger

randomDevNameFile = File
randomDevNameList = ['/dev/random', '/dev/urandom'] -- list of random data source devices
randomDevIStream = InputStream
do
loop dn = 0 to randomDevNameList.length - 1
randomDevNameFile = File(randomDevNameList[dn])
if randomDevNameFile.exists() then leave dn -- We're done! Use this device
randomDevNameFile = null -- ensure we don't use a non-existant device
end dn
if randomDevNameFile == null then signal FileNotFoundException('Cannot locate a random data source device on this system')

-- read 8 bytes from the random data source device, convert it into a BigInteger then display the result
randomBytes = byte[8]
randomDevIStream = BufferedInputStream(FileInputStream(randomDevNameFile))
randomDevIStream.read(randomBytes, 0, randomBytes.length)
randomDevIStream.close()
randomNum = BigInteger(randomBytes)
say Rexx(randomNum.longValue()).right(24) '0x'Rexx(Long.toHexString(randomNum.longValue())).right(16, 0)
catch ex = IOException
ex.printStackTrace()
end
return

/*
To run the program in a loop 10 times from a bash shell prompt use:
for ((i=0; i<10; ++i)); do java <program_name>; done # Shell loop to run the command 10 times
*/
</lang>
'''Output:'''
<pre>
$ for ((i=0; i<10; ++i)); do java RRandomGen; done # Shell loop to run the command 10 times
-3724652236619320966 0xcc4f60865c70f17a
-8287324416757903696 0x8cfd8259e0b94eb0
-2951181559250748016 0xd70b4c02052cfd90
8171526404483923658 0x716717f863fd3eca
-4285529734202916706 0xc486bd699676009e
4783094698411310978 0x4260f74949dc3f82
6972277496665184225 0x60c28171482d97e1
-2382194670272317046 0xdef0be919c96f98a
7952058769071853043 0x6e5b6351938ecdf3
-1857830580859698636 0xe637a8ee0f000234
$
</pre>
=={{header|OCaml}}==
=={{header|OCaml}}==