Advertisement
7_2009-2012 Windows System Services #232917

Uniformly Distributed Random Number Generator

This is a class called urand I wrote to generate random numbers with a perfectly even distribution. If, for example, you want a sequence of 12 random numbers from 0 to 5, this random number generator might put out 3 4 1 0 5 2 5 0 1 4 2 3 whereas a traditional random number generator might output 5 5 0 1 0 4 2 5 3 0 1 3.

AI

KI-Zusammenfassung: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Quellcode
original-source
//---------------------------------------------------------------------------
#ifndef urandH
#define urandH
#include <vector.h>
//---------------------------------------------------------------------------
class urand
{
  public:
    urand();
    urand(double start, double end, double increment = 1);
    explicit urand(vector<int> NumList);
    explicit urand(vector<double> NumList);
    double Rand();
    void prime(double start, double end, double increment = 1);
    void prime(vector<int> NumList);
    void prime(vector<double> NumList);
    void reset();
  private:
    vector<double> RandNums;
    int iter;
    void error();
    void shuffle();
};
#endif
//---------------------------------------------------------------------------
#include "urand.h"
#include <stdlib.h>
#include <vector.h>
#include <iostream.h>
#include <conio.h>
//---------------------------------------------------------------------------
urand::urand()
{
  prime(0,0);
}
/******************************************************************************/
urand::urand(double start, double end, double increment)
{
  prime(start, end, increment);
}
/******************************************************************************/
urand::urand(vector<int> NumList)
{
  prime(NumList);
}
/******************************************************************************/
urand::urand(vector<double> NumList)
{
  prime(NumList);
}
/******************************************************************************/
double urand::Rand()
{
  if(iter >= RandNums.size())
    reset();
  iter++;
  return(RandNums[iter-1]);
}
/******************************************************************************/
void urand::prime(double start, double end, double increment)
{
  if(end < start || increment <= 0)
  {
    error();
    return;
  }
  randomize();
  iter = 0;
  RandNums.clear();
  for(double i = start; i <= end; i+=increment)
    RandNums.push_back(i);
  shuffle();
}
/******************************************************************************/
void urand::prime(vector<int> NumList)
{
  if(NumList.size() == 0)
    prime(0,0);
  else
  {
    RandNums.clear();
    for(int i = 0; i < NumList.size(); i++)
      RandNums.push_back(NumList[i]);
  }
  shuffle();
}
/******************************************************************************/
void urand::prime(vector<double> NumList)
{
  if(NumList.size() == 0)
    prime(0,0);
  else
    RandNums = NumList;
  shuffle();
}
/******************************************************************************/
void urand::reset()
{
  iter = 0;
  shuffle();
}
/******************************************************************************/
void urand::error()
{
  cout << "Invalid use of urand object.\n";
  getch();
}
/******************************************************************************/
void urand::shuffle()
{
  randomize();
  int RandPos;
  double dummy;
  for(int i = 0; i < RandNums.size(); i++)
  {
    RandPos = rand()%RandNums.size();
    dummy = RandNums[i];
    RandNums[i] = RandNums[RandPos];
    RandNums[RandPos] = dummy;
  }
}
/*
//Example Code
int main(int argc, char* argv[])
{
  urand x(0, 15);
  for(int i = 0; i < 16; i++)
    cout << x.Rand() << " ";
  cout << endl << endl;
  x.prime(5, 9);
  for(int i = 0; i < 5; i++)
    cout << x.Rand() << " ";
  cout << endl << endl;
  cout << x.Rand() << " " << x.Rand() << " " << x.Rand() << " ";
  x.reset();
  cout << x.Rand() << " " << x.Rand() << " " << x.Rand() << " ";
  cout << endl << endl;
  x.prime(4, 5, .1);
  for(int i = 0; i < 11; i++)
    cout << x.Rand() << " ";
  cout << endl << endl;
  vector<int> nums(10);
  for(int i = 0; i < 10; i++)
    nums[i] = i*i;
  x.prime(nums);
  for(int i = 0; i < 10; i++)
    cout << x.Rand() << " ";
  cout << endl << endl;
  getch();
  return 0;
}
*/
Upload
Originalkommentare (3)
Wiederhergestellt von der Wayback Machine