Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Time Intervals

This thread is locked; no one can reply to it. rss feed Print
Time Intervals
AceBlkwell
Member #13,038
July 2011
avatar

All,

Years ago I created a small function to return random numbers. The calling function would send a limit and the return would send a number between 0 and the limit. I used a time function to set the seed. It's worked pretty good over the years until now.

I have a test program for randomly locating pixels to the screen. The program writes random numbers to an array. It then uses the array to locate the pixels. The issue is the random number to the array loops so fast that the results are uniformed. I figure it's time to get away from the C time function and look for a newer one. I briefly read something about nanoseconds with a C++ call. Any recommendations? Is there a more current way of creating random numbers?

#SelectExpand
1#include<iostream> 2#include<cstdio> 3#include<cstdlib> 4#include<time.h> 5#include <sys/time.h> 6 7using namespace std; 8 9int random_no(int limit){ 10 11 char ct[28]; 12 struct timeval tv; 13 unsigned int number,modnumber; 14 long int seed; 15 static int offset = 0 ; 16 char run; 17 18 seed= time(NULL); 19 20// gettimeofday(&tv, NULL); 21// seed = tv.tv_usec; 22 23 srand(seed+offset); 24 25 modnumber = rand()%limit; 26 offset++; 27 return(modnumber); 28 29} //end random_no()

torhu
Member #2,727
September 2002
avatar

The issue is the random number to the array loops so fast that the results are uniformed.

You should call srand just once, not every time you want a new random number. srand resets the number sequence that rand produces. So if you end up with the same seed, you get the same output.

Dizzy Egg
Member #10,824
March 2009
avatar

What torhu said! You could use a static bool in your function so you can keep calling it when needed but it only seeds the first time.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

MiquelFire
Member #3,110
January 2003
avatar

AFAIC, srand is something you call in main() once at the start, most often one of the first lines you call. It's in the same category as al_init()

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

AceBlkwell
Member #13,038
July 2011
avatar

Thanks all, I'll give that a try. I figured there had to be a better approach.

After the advice, I was thinking along the same lines as you Dizzy. I want to keep the function self-contained. Thanks again all.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter Hull
Member #1,136
March 2001

If you're in a C++ mood you could try:

#include <random>

int random_no(int limit) {
  static std::minstd_rand gen;
  return std::uniform_int_distribution<int>(0, limit-1)(gen);
}

Supposedly minstd_rand is a better random number generator than rand(). There are a few internet articles saying don't use rand()%limit and a few more saying it doesn't matter.

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Peter, I figured there had to be a more current command.

As of now though, I updated my code to include a bool that limits the seeding to one time. It seems to work well. I also caught that I have a couple variables declared that I don't use for anything. I removed them (int number & char run) Must have used them years ago or was at least trialing a new approach back in the day.

Meanwhile, can you tell me if your approach would eliminate the need for different time functions, depending on while compiler I'm using? IE no need to seed?

Edgar, was this what you were mentioning as well?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Edgar. I just read the Wiki. Worth looking into deeper. I'll check it out.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: