![]() |
|
Time Intervals |
AceBlkwell
Member #13,038
July 2011
![]() |
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? 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
![]() |
AceBlkwell said: 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
![]() |
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.
---------------------------------------------------- |
MiquelFire
Member #3,110
January 2003
![]() |
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() --- |
AceBlkwell
Member #13,038
July 2011
![]() |
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
![]() |
srand and rand is sufficient for most purposes. The mersenne twister PRNG (Psuedo random number generator) provides better more random results across ranges of values. And you seed and poll it just like rand.
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
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
![]() |
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
![]() |
Mersenne twister is in c++11. Pretty simple use, just google it. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
AceBlkwell
Member #13,038
July 2011
![]() |
Thanks Edgar. I just read the Wiki. Worth looking into deeper. I'll check it out. |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
You can see how I wrapped it to get regular numbers in my Eagle library. https://github.com/EdgarReynaldo/EagleGUI/blob/master/src/Eagle/Random.cpp My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|