While working on a program for the GDK Challenge thread, I realized how aweful dbRnd() is. I noticed that every time I run the program, it produces the same set of random integers. This wasn't going to work for my program so I set out and found a way to code another int generator.
I found a nice site (which I give credit too in the function) that explained how to make one so I coded it out. Unfortunately it had about the same effect as dbRnd(), so I made some changes. Below is a simple but powerful little int generator. Use it as you please, imo, a much better substitute to dbRnd. Enjoy
In order to run this code, you must include cstdlib & time.h
// ========================
// | Random Int Generator |
// | Author: Mason Cook |
// ===================================================================
// | Credit(s): RoD http://www.cprogramming.com/tutorial/random.html |
// ===================================================================
// | Creates a Random Integer from seed & system time |
// ====================================================
// | rMin (int): The minimum value for the random int |
// | rMax (int): The maximum value for the random int |
// ====================================================
int paRndInt(int rMin, int rMax)
{
// Get Clock Time for seed
time_t seed;
time(&seed);
seed = seed + rand();
// Get Random int from seed
srand((unsigned int) seed);
// Make sure number is between min-max
return rand() % (rMin - rMax + 1) + rMin;
}
And here is a sample program to see how its used.
// Header Includes
#include "DarkGDK.h"
#include <cstdlib>
#include <time.h>
// ========================
// | Random Int Generator |
// | Author: Mason Cook |
// ===================================================================
// | Credit(s): RoD http://www.cprogramming.com/tutorial/random.html |
// ===================================================================
// | Creates a Random Integer from seed & system time |
// ====================================================
// | rMin (int): The minimum value for the random int |
// | rMax (int): The maximum value for the random int |
// ====================================================
int paRndInt(int rMin, int rMax)
{
// Get Clock Time for seed
time_t seed;
time(&seed);
seed = seed + rand();
// Get Random int from seed
srand((unsigned int) seed);
// Make sure number is between min-max
return rand() % (rMin - rMax + 1) + rMin;
}
// Main Entry GDK
void DarkGDK ( void )
{
// Variables
const int min(1), max(145);
int rndTest(0);
// Setup Refresh Rate
dbSyncOn();
dbSyncRate(60);
// Get Rnd
for(int x = 0; x != 10; x++)
dbPrint(dbStr(paRndInt(min, max)));
// GDK Loop
while (LoopGDK())
{
// Refresh Screen
dbSync();
}
// Exit
return;
}