Skip to content

Instantly share code, notes, and snippets.

@louisaslett
Last active April 25, 2024 10:41
Show Gist options
  • Save louisaslett/84f4126474ae848f4d70fb64c2e77008 to your computer and use it in GitHub Desktop.
Save louisaslett/84f4126474ae848f4d70fb64c2e77008 to your computer and use it in GitHub Desktop.
Simple test for coolbutuseless/cryptorng
---
title: ""
format: html
---
[https://fosstodon.org/@coolbutuseless/112329375557579427](https://fosstodon.org/@coolbutuseless/112329375557579427)
```{r}
# Quick script to do a simple test of uniform byte generator in package
# coolbutuseless/cryptorng which uses OS level cryptographic RNGs
# install.packages('remotes')
# remotes::install_github('coolbutuseless/cryptorng')
library("cryptorng")
# Note 00 00 00 80 is NA, so one possible generated value could get NA
x <- na.omit(replicate(10000,
packBits(rawToBits(generate_bytes(4)),
"integer")))
# Integer range is -2^31+1 to 2^31-1 in R (NB -2^31 is not the smallest int as
# a value is reserved to represent NA)
# Transform the integers to interval [0,1]
u <- (x / (2*(2^31-1))) + 0.5
# Perform basic test of uniformity (not the most powerful ... maybe someone
# would like to extend to use diehard?)
ks.test(u, punif)
# OS/Architecture/Platform/R version
c(currentos = utils::osVersion, R.Version()[c('arch', 'os', 'version.string')])
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment