Skip to content

Commit

Permalink
software: add prng
Browse files Browse the repository at this point in the history
  • Loading branch information
herbertp authored and rroohhh committed Mar 4, 2024
1 parent 66d37e5 commit 5aeb9d4
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions software/prng/prng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**********************************************************************
** prng.c
** Pseudo Random Number Generator
** Version 1.2
**
** Copyright (C) 2014-2020 H.Poetzl
**
** This program is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, either version
** 2 of the License, or (at your option) any later version.
**
**********************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define BUF_SIZE 4096

static uint64_t buf[BUF_SIZE];

static inline uint64_t
xoroshiro128plus(uint64_t s[2])
{
uint64_t s0 = s[0];
uint64_t s1 = s[1];
uint64_t result = s0 + s1;
s1 ^= s0;
s[0] = ((s0 << 55) | (s0 >> 9)) ^ s1 ^ (s1 << 14);
s[1] = (s1 << 36) | (s1 >> 28);
return result;
}

int main(int argc, char *argv[])
{
uint64_t s[2];

int fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open seed");
exit(1);
}

ssize_t len = read(fd, s, sizeof(s));
if (len != sizeof(s)) {
perror("read seed");
exit(2);
}
close(fd);

do {
for (int i=0; i<BUF_SIZE; i++)
buf[i] = xoroshiro128plus(s);

len = write(1, buf, sizeof(buf));
} while (len == sizeof(buf));

exit(0);
}

0 comments on commit 5aeb9d4

Please sign in to comment.