Created
November 6, 2021 21:49
-
-
Save dereckmezquita/a5b94f6cd7c10a9c7ba314b5a074af48 to your computer and use it in GitHub Desktop.
Code for randomly selecting random numbers between an integer and tallying up results. Reference for generating random numbers. R used for visualisation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
#include <iostream> | |
#include <random> | |
#include <chrono> | |
using namespace Rcpp; | |
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); | |
// [[Rcpp::export]] | |
NumericVector randomChoose(int iter, int sample) { | |
NumericVector collected(sample, 0.0); | |
for(int i = 1; i <= iter; i++) { | |
collected[std::uniform_int_distribution<int>(0, sample)(rng)]++; | |
} | |
return collected; | |
} | |
// [[Rcpp::export]] | |
List iterRandomChoose(int runs, int by) { | |
List results(runs / by); | |
for(int i = 0; i < runs; i += by) { | |
std::cout << i << std::endl; | |
// std::cout << i / by << std::endl; | |
results[i / by] = randomChoose(i, 10); | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code used for visualisation:
Brew install imagemagik:
convert -delay 20 -loop 0 `ls | sort -n` random-number-selection.gif