Skip to content

Commit d6ae5a9

Browse files
committed
Use native random numbers rather than R::runif to control sampling
1 parent 47f785d commit d6ae5a9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/read_mismatchbam_cpp.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <Rcpp.h>
1212
#include <cli/progress.h>
1313
#include "utils.h"
14+
#include <random>
1415

1516
#define MISMATCHBAM_MODE_READ 1
1617
#define MISMATCHBAM_MODE_STATE 3
@@ -524,6 +525,13 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str,
524525

525526
// ... return value for mode 3
526527
Rcpp::NumericMatrix pair_counts;
528+
// random number generation
529+
// first generate a single random number from R
530+
// to link the C++ RNG to the R session's current seed state
531+
// dis() can then be used to draw uniformly distributed random numbers
532+
double r_seed = R::runif(0, 1000000);
533+
std::mt19937 gen((unsigned int)r_seed);
534+
std::uniform_real_distribution<double> dis(0.0, 1.0);
527535

528536
// prepare bam file for reading
529537
if (verbose) {
@@ -603,7 +611,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str,
603611
(calculate_aligned_bases(bamdata) >= minAlignedLength)) {
604612

605613
if (!(bamdata->core.flag & BAM_FPAIRED) &&
606-
((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction))) {
614+
((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction))) {
607615
// single-end - process directly
608616
success = process_mismatch_bam_record(
609617
MISMATCHBAM_MODE_STATE, // run mode
@@ -663,7 +671,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str,
663671
// mate seen - get it from the map and process the pair
664672
bamdata2 = curr_records_it->second;
665673

666-
if ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction)) {
674+
if ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction)) {
667675
success = process_mismatch_bam_record_pair(
668676
MISMATCHBAM_MODE_STATE, // run mode
669677
bamdata2, // bam record
@@ -712,7 +720,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str,
712720
curr_records_it++) {
713721
bamdata2 = curr_records_it->second;
714722

715-
if ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction)) {
723+
if ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction)) {
716724
success = process_mismatch_bam_record(
717725
MISMATCHBAM_MODE_STATE, // run mode
718726
bamdata2, // bam record
@@ -802,7 +810,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str,
802810
// read overlapping alignments using iterator
803811
while ((c = sam_itr_next(infile, iter, bamdata)) >= 0) {
804812
if (!(bamdata->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FSUPPLEMENTARY)) &&
805-
((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction))) {
813+
((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction))) {
806814
success = process_mismatch_bam_record(
807815
MISMATCHBAM_MODE_READ, // run mode
808816
bamdata, // bam record

0 commit comments

Comments
 (0)