Quake3World.com Forums
     Programming Discussion
        Generating a random number in q3_ui


Post new topicReply to topic
Login | Profile | | FAQ | Search | IRC




Print view Previous topic | Next topic 
Topic Starter Topic: Generating a random number in q3_ui

Cool #9
Cool #9
Joined: 01 Dec 2000
Posts: 44139
PostPosted: 05-24-2011 12:20 AM           Profile   Send private message  E-mail  Edit post Reply with quote


I've succesfully used code like this:

Code:
int i;
i = rand() % 4;


in cgame where this would generate a proper random number for me.
In q3_ui (more specifically in ui_main.c) I'm trying to use the same code to generate a random number, but for some reason, it always generates the same number for me. If I leave out the "% 4" bit, then it always generates the number 41 for me. Why is this not working? Do I need to manually initialize some seed or something somewhere? And if so, how?




Top
                 

Immortal
Immortal
Joined: 12 Mar 2005
Posts: 2205
PostPosted: 05-24-2011 03:10 AM           Profile   Send private message  E-mail  Edit post Reply with quote


sounds to me like it's not being seeded properly before use, like you've said.

Usually the current time on program start is used as the seed before rand() is called.

It's set such as srand(value);

I would imagine you could just include "time.h" and call srand(time(NULL)); in q3_ui's entry point.




Top
                 

Mentor
Mentor
Joined: 12 Mar 2005
Posts: 3958
PostPosted: 05-24-2011 03:27 AM           Profile Send private message  E-mail  Edit post Reply with quote


You can't use system headers. game and cgame use level.time to seed the generator but for q3_ui you need to declare trap_RealTime():
Code:
int trap_RealTime(qtime_t *qtime) {
        return syscall( UI_REAL_TIME, qtime );
}

Apropos:

Image

Image




Top
                 

Cool #9
Cool #9
Joined: 01 Dec 2000
Posts: 44139
PostPosted: 05-24-2011 04:14 AM           Profile   Send private message  E-mail  Edit post Reply with quote


Urm, what do you mean I need to declare it? It's already there in cg_syscalls.c and declared in ui_local.h
Did you mean to say I need to call it? And what is the qtime argument and what does it return?

edit:
nm, doing this seems random enough for me (i guess it returns the current UTC time?):

Code:
int r;
qtime_t *qtime;
r = trap_RealTime(qtime) % 4;


Not sure if that's what you meant though




Top
                 

Mentor
Mentor
Joined: 12 Mar 2005
Posts: 3958
PostPosted: 05-24-2011 06:29 AM           Profile Send private message  E-mail  Edit post Reply with quote


Hah, that's randomness of the kind you don't want: in your example, the engine writes data to whatever that uninitialized pointer points to.

You need to pass it a pointer to a qtime_t struct, like this:
Code:
qtime_t tm;
int seed;

trap_RealTime(&tm);
seed = 1;
seed = seed * 31 + tm.tm_sec;
seed = seed * 31 + tm.tm_min;
seed = seed * 31 + tm.tm_hour;
seed = seed * 31 + tm.tm_mday;
srand(seed);




Top
                 

Cool #9
Cool #9
Joined: 01 Dec 2000
Posts: 44139
PostPosted: 05-24-2011 06:38 AM           Profile   Send private message  E-mail  Edit post Reply with quote


ok, got it to work. Thanks.




Top
                 
Quake3World.com | Forum Index | Programming Discussion


Post new topic Reply to topic


cron
Quake3World.com
© ZeniMax. Zenimax, QUAKE III ARENA, Id Software and associated trademarks are trademarks of the ZeniMax group of companies. All rights reserved.
This is an unofficial fan website without any affiliation with or endorsement by ZeniMax.
All views and opinions expressed are those of the author.