Random number generator (device): Difference between revisions
m (→Tcl: Change default device based on comments on Talk page) |
(J draft) |
||
Line 2: | Line 2: | ||
If your system has a means to generate random numbers involving not only a software algorithm (like the [[wp:/dev/random|/dev/random]] devices in Unix), show how to obtain a random 32-bit number from that mechanism. |
If your system has a means to generate random numbers involving not only a software algorithm (like the [[wp:/dev/random|/dev/random]] devices in Unix), show how to obtain a random 32-bit number from that mechanism. |
||
=={{header|J}}== |
|||
Untested: |
|||
<lang j>256#.a.i.1!:11'/dev/urandom';0 4</lang> |
|||
Fallback: |
|||
<lang j>256#.a.i.4{.host'dd if=/dev/random bs=4 count=1'</lang> |
|||
Note: this assumes that J is running on linux. |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<lang PicoLisp>: (in "/dev/urandom" (rd 4)) |
<lang PicoLisp>: (in "/dev/urandom" (rd 4)) |
||
-> 2917110327</lang> |
-> 2917110327</lang> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
PureBasic has the source for the random data is the "/dev/urandom" device on Linux or Mac OSX and the "Microsoft Cryptography API" on Windows. |
PureBasic has the source for the random data is the "/dev/urandom" device on Linux or Mac OSX and the "Microsoft Cryptography API" on Windows. |
Revision as of 16:41, 11 January 2011
If your system has a means to generate random numbers involving not only a software algorithm (like the /dev/random devices in Unix), show how to obtain a random 32-bit number from that mechanism.
J
Untested: <lang j>256#.a.i.1!:11'/dev/urandom';0 4</lang>
Fallback: <lang j>256#.a.i.4{.host'dd if=/dev/random bs=4 count=1'</lang>
Note: this assumes that J is running on linux.
PicoLisp
<lang PicoLisp>: (in "/dev/urandom" (rd 4)) -> 2917110327</lang>
PureBasic
PureBasic has the source for the random data is the "/dev/urandom" device on Linux or Mac OSX and the "Microsoft Cryptography API" on Windows. <lang PureBasic>If OpenCryptRandom()
MyRandom = CryptRandom(#MAXLONG) CloseCryptRandom()
EndIf</lang>
Tcl
<lang tcl>package require Tcl 8.5
- Allow override of device name
proc systemRandomInteger Template:Device "/dev/random" {
set f [open $device "rb"] binary scan [read $f 4] "I" x close $f return $x
}</lang> Usage: <lang tcl>% puts [systemRandomInteger] 636131349</lang>