Important alert: (current site time 9/1/2014 9:16:02 AM EDT)
 

VB icon

Uniformly Distributed Random Number Generator

Email
Submitted on: 7/17/2002 12:38:30 AM
By: Bomb Atomically 
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 12457
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Uniformly Distributed Random Number Generator
// Description: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.
// By: Bomb Atomically
//
// Inputs:A urand object can be constructed from a start and an end value (e.g. urand x(0, 4); would generate random numbers from the list 0,1,2,3,4), a start value, end value, and increment (e.g. urand x(1, 1.3, .1); would generate random numbers from the list 1,1.1,1.2,1.3), or a vector of integers or a vector of doubles (the random number generator would randomly select numbers from the vector).
//
// Returns:The Rand() member function of urand returns a single random value from the set of random numbers defined by the user.
//
// Assumes:Once a urand object is primed for a certain set of numbers, it will keep generating numbers from that list until it is primed for another list. You can generate a random number in one function, pass the urand object to another function, generate some more numbers, etc. and it will still spit out numbers with perfect distribution. Also, if you prime a urand object with a vector, that vector does not need to be in scope for the random number generator to function properly. If you have any questions, comments, suggestions, etc. please e-mail me at bomb_atomically@hotmail.com.
//
// Side Effects:The random number generator will always exhaust the entire set of numbers defined by the user before repeating itself. This means that the generator is slightly deterministic; the last number output by the random number generator gives information about the next number it will output. While this is good for some applications, it is bad for others. Do not use this random number generator to, for example, randomly generate a binary number.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4385&lngWId=3//for details.//**************************************

//---------------------------------------------------------------------------
#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;
}
*/


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/10/2004 6:49:40 AMJohn Bell

There is a fundemental flaw here with this concept. What this code generates is a VERY pseudorandom sequence. The concept of forcing a distribution spread on a random sequence nullifys the very idea of randomness. =0)
(If this comment was disrespectful, please report it.)

 
7/21/2006 7:21:16 AMdeepak

exellent
(If this comment was disrespectful, please report it.)

 
6/28/2008 2:07:58 PMjohn

Thank you!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.