tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1 @@
If your system has a means to generate random numbers involving not only a software algorithm (like the [[wp:/dev/random|/dev/urandom]] devices in Unix), show how to obtain a random 32-bit number from that mechanism.

View file

@ -0,0 +1,3 @@
---
category:
- Input Output

View file

@ -0,0 +1,13 @@
with Ada.Streams.Stream_IO;
with Ada.Text_IO;
procedure Random is
Number : Integer;
Random_File : Ada.Streams.Stream_IO.File_Type;
begin
Ada.Streams.Stream_IO.Open (File => Random_File,
Mode => Ada.Streams.Stream_IO.In_File,
Name => "/dev/random");
Integer'Read (Ada.Streams.Stream_IO.Stream (Random_File), Number);
Ada.Streams.Stream_IO.Close (Random_File);
Ada.Text_IO.Put_Line ("Number:" & Integer'Image (Number));
end Random;

View file

@ -0,0 +1,2 @@
SYS "SystemFunction036", ^random%, 4
PRINT ~random%

View file

@ -0,0 +1,14 @@
using System;
using System.Security.Cryptography;
private static int GetRandomInt()
{
int result = 0;
var rng = new RNGCryptoServiceProvider();
var buffer = new byte[4];
rng.GetBytes(buffer);
result = BitConverter.ToInt32(buffer, 0);
return result;
}

View file

@ -0,0 +1,25 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char **argv)
{
uint32_t v;
FILE *r = fopen("/dev/urandom", "r");
if (r == NULL)
{
perror("/dev/urandom");
return 1;
}
size_t br = fread(&v, sizeof v, 1, r);
if (br < 1)
{
fputs("/dev/urandom: Not enough bytes\n", stderr);
return 1;
}
printf("%" PRIu32 "\n", v);
fclose(r);
return 0;
}

View file

@ -0,0 +1,10 @@
#include <inttypes.h> /* PRIu32 */
#include <stdlib.h> /* arc4random */
#include <stdio.h> /* printf */
int
main()
{
printf("%" PRIu32 "\n", arc4random());
return 0;
}

View file

@ -0,0 +1,18 @@
#include <inttypes.h>
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
int
main()
{
uint32_t v;
if (RAND_bytes((unsigned char *)&v, sizeof v) == 0) {
ERR_print_errors_fp(stderr);
return 1;
}
printf("%" PRIu32 "\n", v);
return 0;
}

View file

@ -0,0 +1,23 @@
#include <stdio.h> /* printf */
#include <windows.h>
#include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */
int
main()
{
HCRYPTPROV p;
ULONG i;
if (CryptAcquireContext(&p, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
fputs("CryptAcquireContext failed.\n", stderr);
return 1;
}
if (CryptGenRandom(p, sizeof i, (BYTE *)&i) == FALSE) {
fputs("CryptGenRandom failed.\n", stderr);
return 1;
}
printf("%lu\n", i);
CryptReleaseContext(p, 0);
return 0;
}

View file

@ -0,0 +1,9 @@
variable rnd
: randoms ( n -- )
s" /dev/random" r/o open-file throw
swap 0 do
dup rnd 1 cells rot read-file throw drop
rnd @ .
loop
close-file throw ;

View file

@ -0,0 +1,34 @@
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"os"
)
func main() {
testRandom("crypto/rand", rand.Reader)
testRandom("dev/random", newDevRandom())
}
func newDevRandom() (f *os.File) {
var err error
if f, err = os.Open("/dev/random"); err != nil {
panic(err)
}
return
}
func testRandom(label string, src io.Reader) {
fmt.Printf("%s:\n", label)
var r int32
for i := 0; i < 10; i++ {
if err := binary.Read(src, binary.LittleEndian, &r); err != nil {
panic(err)
}
fmt.Print(r, " ")
}
fmt.Println()
}

View file

@ -0,0 +1 @@
def rng = new java.security.SecureRandom()

View file

@ -0,0 +1 @@
(0..4).each { println rng.nextInt() }

View file

@ -0,0 +1,9 @@
#!/usr/bin/runhaskell
import System.Entropy
import Data.Binary.Get
import qualified Data.ByteString.Lazy as B
main = do
bytes <- getEntropy 4
print (runGet getWord32be $ B.fromChunks [bytes])

View file

@ -0,0 +1 @@
256#.a.i.1!:11'/dev/urandom';0 4

View file

@ -0,0 +1 @@
256#.a.i.4{.host'dd if=/dev/urandom bs=4 count=1'

View file

@ -0,0 +1,10 @@
import java.security.SecureRandom;
public class RandomExample {
public static void main(String[] args) {
SecureRandom rng = new SecureRandom();
/* Prints a random signed 32-bit integer. */
System.out.println(rng.nextInt());
}
}

View file

@ -0,0 +1,32 @@
/* 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
*/

View file

@ -0,0 +1,13 @@
let input_rand_int ic =
let i1 = int_of_char (input_char ic)
and i2 = int_of_char (input_char ic)
and i3 = int_of_char (input_char ic)
and i4 = int_of_char (input_char ic) in
i1 lor (i2 lsl 8) lor (i3 lsl 16) lor (i4 lsl 24)
let () =
let ic = open_in "/dev/urandom" in
let ri31 = input_rand_int ic in
close_in ic;
Printf.printf "%d\n" ri31;
;;

View file

@ -0,0 +1,16 @@
let input_rand_int32 ic =
let i1 = Int32.of_int (int_of_char (input_char ic))
and i2 = Int32.of_int (int_of_char (input_char ic))
and i3 = Int32.of_int (int_of_char (input_char ic))
and i4 = Int32.of_int (int_of_char (input_char ic)) in
let i2 = Int32.shift_left i2 8
and i3 = Int32.shift_left i3 16
and i4 = Int32.shift_left i4 24 in
Int32.logor i1 (Int32.logor i2 (Int32.logor i3 i4))
let () =
let ic = open_in "/dev/urandom" in
let ri32 = input_rand_int32 ic in
close_in ic;
Printf.printf "%ld\n" ri32;
;;

View file

@ -0,0 +1,11 @@
program RandomNumberDevice;
var
byteFile: file of byte;
randomByte: byte;
begin
assign(byteFile, '/dev/urandom');
reset (byteFile);
read (byteFile, randomByte);
close (byteFile);
writeln('The random byte is: ', randomByte);
end.

View file

@ -0,0 +1,4 @@
my $UR = open("/dev/urandom", :bin) or die "Can't open /dev/urandom: $!";
my @random-spigot := gather loop { take $UR.read(1024).unpack("L*") }
.say for @random-spigot[^10];

View file

@ -0,0 +1,10 @@
sub read_random {
my $device = '/dev/urandom';
open my $in, "<:raw", $device # :raw because it's not unicode string
or die "Can't open $device: $!";
sysread $in, my $rand, 4 * shift;
unpack('L*', $rand);
}
print "$_\n" for read_random(10);

View file

@ -0,0 +1,2 @@
: (in "/dev/urandom" (rd 4))
-> 2917110327

View file

@ -0,0 +1 @@
printline -random-

View file

@ -0,0 +1,4 @@
If OpenCryptRandom()
MyRandom = CryptRandom(#MAXLONG)
CloseCryptRandom()
EndIf

View file

@ -0,0 +1,3 @@
import random
rand = random.SystemRandom()
rand.randint(1,10)

View file

@ -0,0 +1,9 @@
/*REXX program generates a random 32-bit number using the RANDOM bif.*/
/*───────── The 32-bit random number is unsigned and constructed from */
/*───────── two smaller 16-bit numbers, and it's expressed in decimal.*/
/*───────── Note: the REXX random bif has a maximum range of 100,000.*/
numeric digits 10 /*ensure REXX has enough room. */
_=2**16 /*a handy-dandy constant to have.*/
say random(0,_-1)*_+random(0,_-1) /*gen an unsigned 32-bit random #*/

View file

@ -0,0 +1,2 @@
require 'securerandom'
SecureRandom.random_number(1 << 32)

View file

@ -0,0 +1,9 @@
package require Tcl 8.5
# Allow override of device name
proc systemRandomInteger {{device "/dev/random"}} {
set f [open $device "rb"]
binary scan [read $f 4] "I" x
close $f
return $x
}

View file

@ -0,0 +1,2 @@
% puts [systemRandomInteger]
636131349

View file

@ -0,0 +1,3 @@
code Ran=1;
int R;
R:= Ran($7FFF_FFFF)