![]() |
Random_long(0, ??)
I actually wonder what RANDOM_LONG takes for args.
Is it: RANDOM_LONG(from, TILL) or RANDOM_LONG(from, including) meaning: RANDOM_LONG(0,10) can return 0,1,2,3,4,5,6,7,8,9 or it can return: 0,1,2,3,4,5,6,7,8,9,10 i need to know this because my current function does not seem to return all goals i want, or... it could be i have been darn unfortunate: Code:
// Find a goal, and return the node close to it |
Re: Random_long(0, ??)
I think (and especially hope, I use it VERY much) it will include the lowest and highest number. But you can easily find it out just print every frame a random number between 0 and 10 to an file and play about 5 minutes :)
|
Re: Random_long(0, ??)
yes, i know, i am just to lazy (grtz at pierre ;) ) to put such code in my bot...
|
Re: Random_long(0, ??)
RANDOM_LONG(0,10);
Code:
Random Long from 0 to 10 : 4 |
Re: Random_long(0, ??)
yay :) all numbers are in the list
|
Re: Random_long(0, ??)
else write your own random number generating facility, and don't bother calling functions in the engine for that. I'm considering this with more and more interest, actually :)
|
Re: Random_long(0, ??)
I wanted to ask If RANDOM_LONG (or RANDOM_FLOAT) is better
then using rand() function (eg. is it faster or is it only to ease programming ?) |
Re: Random_long(0, ??)
I agree with what PMB is saying, use as little of the engine as you can. You should try and separate your code from the engine through a clean interface.
I don't know which functions will be better, but my guess is that rand() will be faster to call. The best reason to use your own random_long (etc) is that you can reuse the code for other projects. For example, I built all my own timing functions and I don't use the engine for timing at all. Now I can use these same functions on other projects which is nice. My code is much cleaner too, and problems that some people had with the engine timing never affected me. |
Re: Random_long(0, ??)
Quote:
|
Re: Random_long(0, ??)
Quote:
I once saw some people had problems over at the botmans forums, but now I can't remember exactly what it was. |
Re: Random_long(0, ??)
the frame time reported by the engine is unreliable, that was one of the problems.
Rob, I don't know at all how to use clock() nor generic timers - at all, but I would be very interested in doing something similar. Would you mind posting the code you are using for this (hopefully it will be simple enough for me to understand it all) or am I just a bugger and do you honestly think I need to search on my own ? :D (might be, too) |
Re: Random_long(0, ??)
AFAIK clock() function http://msdn.microsoft.com/library/de..._crt_clock.asp returns processor time used by calling process, so you can't get real time.
For high precision timing, I would use QueryPerformanceCounter http://msdn.microsoft.com/library/de...ncecounter.asp which is btw used in such time sensitive programs as midi sequencers |
Re: Random_long(0, ??)
I don't care about microseconds, really... I just want to find a convenient, self-contained replacement for gpGlobals->time :)
|
Re: Random_long(0, ??)
Quote:
|
Re: Random_long(0, ??)
AFAIK, gpGlobals->time returns the time in seconds since the server booted on a new map, no kludges...
BTW, for the original question, I found a very sophisticated random number generator on the net ! The problem with rand() is that it can only return 16-bit integers, you can't do real 32-bit number generation using rand() ; however some sound libraries use extremely well shaped random number generators for generating sound noise, they are definitely worth a look. |
Re: Random_long(0, ??)
PMB, you are correct, a 16 bit random value may not be enough for some applications. For better random number generation, I found this library GNU licensed http://www.agner.org/random/ I have not tried it yet, but it looked pretty good on first glance.
As for gpGlobals->time I once tried using it to get the remaining time of a map with no luck, so it is not even good for doing that. If anyone knows how to get a reliable "timeleft" value (engine side, not client) let me know. For many apps, clock( ) gives sufficient resolution which is about 1/1000 of a second. But I can see some apps needing a lot more resolution. The QueryPerformanceCounter is very interesting - thanks for mentioning this one. I'm trying to find out how it is implemented. It appears to be based on CPU frequency, or it could be based on a hardware frequency counter. If you can point me to a document that describes how the value is generated I'd appreciate it. NOTE: I'm mostly interested in protable code using ANSI C++ because whatever timer I use has to be made portable at least to Linux. PBM, I have a lot of code for doing my timer class - too much for a post in here I think. I'll post this main function to give you an idea what I'm doing. Code:
tinTicks tclCPUClock::finGetTickCount( ) I added a rollover check to make sure I don't get a bad hicup if my code is left running for more than 24 days. |
Re: Random_long(0, ??)
Quote:
Quote:
Quote:
Quote:
About the code you posted, your idea to keep track of the rollover count is great ; however I think I'll stick with simpler stuff for now, until I understand exactly how this clock thingy works :) But thanks anyway ! |
Re: Random_long(0, ??)
Quote:
I thought I tried exactly what your are suggesting, but I messed around with the code so many times I can't be sure anymore. Perhaps I used the engine time function instead of the gpGlobals->time value? I'll give it another try - thanks :) Quote:
Quote:
The clock() thing is very simple without rollover. It just returns the number of clock ticks (~1000 per second) since the calling process started. To time something, you just get the clock() count for the start time, and later get the clock() end time. Then you compute the difference of the start time with the end time giving you the number of elapsed clock ticks. To get this in seconds, just divide by 1000 (better to use CLK_TCK). What I did was create a timer class so I could set up multiple independant timers. I do something like vclTimer.fvoStart(10) to start counting up to 10 seconds, and I can check if the timer has timedout or not with a simple boolen function, example if ( vclTimer.fbTimedout() ) { // press the fire button else keep waiting } From this timing function, you can create a generic system for doing timed events that get triggered after a certain period of time elapses. Doing that will simplify realtime programming by a huge factor. |
Re: Random_long(0, ??)
just got back to this thread by the search function ...
just to mention : QueryPerformanceCounter is based on the hardware clock, running at ~1Mhz I think. so it is _not_ based on the cpu clock |
All times are GMT +2. The time now is 15:01. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.