🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Random Number Generator (code)

Started by
1 comment, last by Timkin 22 years, 10 months ago
Just include the header in your module that uses the random number generator and link the compiled source file into your project. Call initRand() to initialise the generator and then use either random() to return a random double between 0 and 1 or random(low,high) to obtain a random integer between high and low. NOTE: This code has not been optimised but is rather written for clarity of the algorithm. Feel free to optimise it to your hearts delight. Here's the header file...
      
#ifndef RAND_H
#define RAND_H

void		initRand(void);
void		advanceRand(void);
double	             random(void);
int		random(int low, int high);

#endif
    
  
..and here's the source file.
            
/****************************************************************************
 *  Module RAND.CPP                                                                                                             *
 *  Based on an algorithm by Donald Knuth                                                                                *
 *  (c) 2000 Tim Wilkin.                                                                                                          *
 *  Permission is hereby given to use this as you see fit. I ask only that you                                    *
 *  keep this header information intact.                                                                                     *
 ****************************************************************************/

#include <time.h>
#include "rand.h"
#include <math.h>
#include <iostream>
#include <fstream>


const	int  RAND_ARRAY_LEN=55;		// Length of array of stored random numbers
const int  SPECIAL_INDEX=24;		// Just what the name suggests!

static double	oldRand[RAND_ARRAY_LEN];	// Array of random numbers
static unsigned int	index2Rand = 0;      // Index into array of randoms



/**********************************************
 * Function: initRand. Initialise the random number generator  *      
 **********************************************/
void	initRand(void)
{

   long totalseconds = time(NULL);
   int seconds = static_cast<int>(totalseconds%60L);
	double seed = 0.91999/ seconds;


   double	newRand = 1.0e-9;
   double	prevRand = seed;

   oldRand[RAND_ARRAY_LEN-1] = prevRand;
   for (unsigned int i=0; i<RAND_ARRAY_LEN; i++)
   {
   	unsigned int j = (21*i) % RAND_ARRAY_LEN;
      oldRand[j] = newRand;
      newRand = prevRand - newRand;
      if (newRand < 0.0)
      	newRand += 1.0;
      prevRand = oldRand[j];
   }

   advanceRand();
   advanceRand();
   advanceRand();
   index2Rand = 0;
}


/**********************************************************************
 * Function: advanceRand. Function to generate new random numbers once array is used up *
 **********************************************************************/
void advanceRand(void)
{
	double	newRand;
   for (unsigned int j=0; j<SPECIAL_INDEX; j++)
   {
   	newRand = oldRand[j]-oldRand[j+(RAND_ARRAY_LEN-SPECIAL_INDEX)];
      if (newRand < 0.0)
      	newRand += 1.0;
      oldRand[j] = newRand;
   }
   for (unsigned int j=SPECIAL_INDEX; j<RAND_ARRAY_LEN; j++)
   {
   	newRand = oldRand[j] - oldRand[j-SPECIAL_INDEX];
      if (newRand < 0.0)
      	newRand += 1.0;
      oldRand[j] = newRand;
   }
}

/*******************************************************************
 * Function: random(void). Generate a double precision random number from the set [0,1] *
 *******************************************************************/
double random(void)
{

   index2Rand++;
   if (index2Rand >= RAND_ARRAY_LEN)
   {
   	index2Rand = 0;
      advanceRand();
   }
   return oldRand[index2Rand];
}

/********************************************************************
 * Function: random(low,high). Generate an integer random number from the set [low,high] *
 ********************************************************************/
int	random(int low, int high)
{
	if (low>=high)
   	return low;
	int	tempRand = floor(random()*(double)(high-low+1) + (double)low);
   if (tempRand > high)
   	tempRand = high;

   return tempRand;
}

  
Questions can be directed to me at taw@csse.monash.edu.au Cheers, Timkin Edited by - Timkin on August 28, 2001 1:05:48 PM
Advertisement
quote: Original post by Timkin
Just include the header in your module that uses the random number generator and link the compiled source file into your project. Call initRand() to initialise the generator and then use either random() to return a random double between 0 and 1 or random(low,high) to obtain a random integer between high and low.

NOTE: This code has not been optimised but is rather written for clarity of the algorithm. Feel free to optimise it to your hearts delight.

Here''s the header file...
.....

.....



This is a cool utility, but is it AI? (Okay, for some games it almost certainly is.....)





Ferretman

ferretman@gameai.com
www.gameai.com

From the High Mountains of Colorado

Ferretman
ferretman@gameai.com
From the High Mountains of Colorado
GameAI.Com

quote: Original post by Ferretman

This is a cool utility, but is it AI? (Okay, for some games it almost certainly is.....)


You are correct Steve, this is not AI. However, it was posted in response to the thread on random numbers (which were being used in an AI context I believe!) where I said I would supply code for a random number generator. I should have prefaced the post (or indicated in the subject) that this thread was an addendum to that thread... my apologies.

Feel free to move the post as you see fit, but please let people know where you move it to.

Cheers,

Timkin

(BTW: I have to say I hate the automatted formatting around here... that code looks AWFUL!)

Edited by - Timkin on August 28, 2001 11:28:54 PM

This topic is closed to new replies.

Advertisement