How can I generate a specific set of numbers using a pseudorandom number generator? [closed]

与世无争的帅哥 提交于 2019-12-02 18:14:55

问题


I am frustrated because I do not know if what I am attempting is even possible.

Lets say I have a list of unsigned numbers. I want to create a seed for a certain pseudorandom number generator algorithm that can generate that specific set of numbers.

When creating the seed, I know the length of the list, and the minimum/maximum numbers.

Here is my current algorithm for generating numbers (C++):

unsigned short rng(unsigned short lowerBounds, unsigned short upperBounds){
    static unsigned short lim = (upperBounds - lowerBounds)+1;
    static unsigned short a = 1; //SEED
    a = a*2 % 32749;
    return (a % lim)+lowerBounds;
}

So for example, I would have the numbers { 63, 37, 82, 34, 75}

I would need to have a seed that would generate those numbers in the first 5 runs (order doesnt matter i suppose)

In simpler terms, I want to control the numbers that an RNG generates


回答1:


You could use std::shuffle() if your compiler supports C++11. See the following example:

#include <algorithm>    // std::move_backward
#include <array>        // std::array
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock

int main () {
  // generate your own std::vector or std::array with your own data here
  std::array<int,5> foo {63, 37, 82, 34, 75};

  // obtain a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

  shuffle (foo.begin(), foo.end(), std::default_random_engine(seed));

  for (int& i: foo) {
    // put your function here
    your_function(i);
  }

  return 0;
}

If your compiler doesn't support C++11 features, you could use std::random_shuffle. See the following example:

#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand

// random generator function:
int myrandom (int i) { return std::rand()%i;}

int main () {
  std::srand ( unsigned ( std::time(0) ) );
  std::vector<std::size_t> myvector;

  // set your values in myvector somehow :)
  myvector.push_back(63);
  myvector.push_back(37);
  myvector.push_back(82);
  myvector.push_back(34);
  myvector.push_back(75);

  // using built-in random generator:
  std::random_shuffle ( myvector.begin(), myvector.end() );

  // using myrandom:
  std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);

  for (std::vector<std::size_t>::iterator it=myvector.begin(); it!=myvector.end(); ++it) {
    // put your function here
    your_function(*it);
  }

  return 0;
}



回答2:


code first

#include <random>
#include <iostream>

int f(int low, int high)
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(low, high);

    return dis(gen);
}

std::uniform_int_distribution will give you a random number in your range this way. If your seed is known as well as the length, here is what you do

std::vector<int> f(int low, int high, unsigned seed, int len)
{
    std::vector<int> ret; // RVO applies, no worries
    std::mt19937 gen(seed);
    std::uniform_int_distribution<> dis(low, high);

    for (int i(0); i<len; ++i) {
        ret.push_back(dis(gen));
    }
    return ret;
}


来源:https://stackoverflow.com/questions/21723437/how-can-i-generate-a-specific-set-of-numbers-using-a-pseudorandom-number-generat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!