[Return] [Catalog]

1 guest@cc 1969-12-31T17:00:00
Create a script that creates a random 6 char hash in every language you know. Post your script ITT.

My version (written in PHP):

<?php
echo substr(str_shuffle("aBcEeFgHiJkLmNoPqRstUvWxYz0123456789"), 0, 6);
?>
»
2 guest@cc 1969-12-31T17:00:00
Define "hash". I'll just read that as printable ASCII:

No standard library (unistd is not part of the standard)
#include <unistd.h>
#include <sys/syscall.h>

#define stdout 2
int main()
{
\tunsigned char buf[7];
\tint offset = sizeof buf - 1;
\tbuf[offset] = '\n';
\tsyscall(SYS_getrandom, buf, offset, 0);
\tfor (int i = 0; i < offset; i++) {
\t\tbuf[i] %= '~';
\t\tif (buf[i] < '!')
\t\t\tbuf[i] += '!';
\t}

\twrite(stdout, buf, sizeof buf);
\treturn 0;
}

Prints random 6 char Unicode string
import System.Random
import Data.Char
main :: IO ()
main = putStrLn =<< take 6 . filter isPrint . randoms <$> getStdGen

>Post body has too many lines

gay
»
3 guest@cc 1969-12-31T17:00:00
>>2
Xorshift actually works
// compile with rustc -O to get rid of overflow checking
use std::time;

const NCHAR: usize = 6;

pub struct XorshiftGen {
state: [u64; 2],
}

impl XorshiftGen {
pub fn new() -> XorshiftGen {
let mut state: [u64; 2] = [0; 2];

let t = time::SystemTime::now();
let dur = t.duration_since(time::UNIX_EPOCH).unwrap();
state[0] = dur.as_secs();
state[1] = dur.subsec_nanos() as u64;

XorshiftGen{state: state}
}

pub fn u32(&mut self) -> u32 {
let mut x = self.state[0];
let y = self.state[1];
self.state[0] = y;
x ^= x << 23;
self.state[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
(self.state[1] + y) as u32
}
}

fn main() {
let mut gen = XorshiftGen::new();

let chars: Vec<u8> = (33..126).collect();
let len = chars.len();

let ret: Vec<u8> = (0..NCHAR).map(|_| {
let rand = gen.u32() % len as u32;
chars[rand as usize]
}).collect();

println!("{}", std::str::from_utf8(&ret).unwrap());
}

And now I've run out of ideas.
»
4 guest@cc 1969-12-31T17:00:00
Python:
from random import choice
for i in range(6):
print(choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'),end='')

>>2
The current line restriction does seem pretty small now that I think about programming threads.
I'll change it from 60 to 200. Is that good or do you want it bigger? I think some line restriction is needed though since the maximum character count is 8000.

[Return] [Catalog]
Delete Post:
OptionsPassword
Name
Comment
File