Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one write a repeatable pseudo random number generator? [closed]

For example:

If rprng(seed,index) is my function, then for any pair of (seed,index), I should always get the same value for a given (seed,index).

For example:

rprng(4,2) = 17
rprng(4,5) = 21
rprng(4,2) = 17 
like image 940
Rahul Iyer Avatar asked Apr 25 '26 07:04

Rahul Iyer


1 Answers

A simple idea is to use a PRNG that is so thorough that the values generated by seed, seed+1, seed+2... are acceptably random. E.g.:

#include <random>

unsigned prng(unsigned seed, unsigned index)
{
  thread_local std::mt19937 engine;  // or a different engine
  engine.seed(seed + index);

  return engine();
}

Also check this thread: https://mathoverflow.net/questions/104915/pseudo-random-algorithm-allowing-o1-computation-of-nth-element

like image 118
manlio Avatar answered Apr 28 '26 01:04

manlio