Advertisement

Need help...... stupid problem.......

Started by April 14, 2001 09:17 PM
6 comments, last by Cobra 23 years, 10 months ago
Hey...... I hope one of u can help me... its a really really silly problem... I just cant remember how to do it.... If u use rand()% it runs through a series of values that its assigned ( e.g rand()%256 will pick a random number between them, then pick another.. then another.. then another..... right..... ) How do i get it to just pick ONE random number on runtime, and keep that one till the end? This is really bugging me... cause I know it''s simple..... Thanx in advance for any help. ~Cobra~
"Build a man a fire, and he will be warm for a day. Set a man on fire, and he will have warmth for the rest of his life"
Ummm.. Can't you just store that value somewhere and use it later?

I think I know what you want to do. When I want to randomly position things, I go through a loop and save the random locations. So later, when I want to redraw the same thing(s), they keep their initial positions. I use it for trees, rain, snow, hail, etc...

Jinushaun

Edited by - jinushaun on April 14, 2001 10:42:18 PM
Advertisement
Maybe if I be more specific......

What I am doing is this.....

When u exit my program, I want it to pick a random picture and use that for the backdrop of the credits ( that way its not so boring ).


I can get it to go through random pictures using rand()3%, but then it just flicks between them randomly really fast....

I need it to pick one... and keep that...



Thats all...

Thanx in advance....

~Cobra~
"Build a man a fire, and he will be warm for a day. Set a man on fire, and he will have warmth for the rest of his life"
make a global variable. And when you do an initiation function (something should only run once), make the global thsi:

global=rand()%256;

that will stay a constant for the whole program as long as you don''t touch it

------------------------------
ShiningKnightDX
NovaStorm Games
Project: More demos that you can shake a stick at
Nah... that''d just loop the 256 thing ( whatever it is u have 256 of.. )


Here.... I''ll post the code thats giving me jip hehehe.

//--------------------------------------------------------------

if(BACKGROUND_SHADER)
{
int bg = rand()%3;

if (bg==1)
{
UI_DrawHandlePic(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BackgroundShader);
}

if (bg==2)
{
UI_DrawHandlePic(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BackgroundShader2);
}

else if (bg==3)
{
UI_DrawHandlePic(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BackgroundShader3);
}
}

//-----------------------------------------------------------

Then later on..

//-----------------------------------------------------------

if(BACKGROUND_SHADER)
{

BackgroundShader = trap_R_RegisterShaderNoMip("shader/shader.jpg");
BackgroundShader2 = trap_R_RegisterShaderNoMip("shader/shader2.jpg");
BackgroundShader3 = trap_R_RegisterShaderNoMip("shader/shader3.jpg");
}

//-----------------------------------------------------------

that code works fine, but it runs through the 3 shaders.... ( 3 pics )
I need to modify that, so that it only picks ONE shader, and sticks with it....


Can someone help.... thx.

~Cobra~

"Build a man a fire, and he will be warm for a day. Set a man on fire, and he will have warmth for the rest of his life"
heya,

see where you use rand() ? your constantly getting a new random value, because everything the function is passed, it generates a new bg value.. what u want to do is make bg global and run the bg = rand()%3; in the initialization process, just as mentioned before. therefore, bg will keep the random value throught.

Scott
Advertisement
Just wondering ... how often does

"if(BACKGROUND_SHADER){...}"

get called? If it is in a loop of some sort (that is, if you''re trying to catch a message, which it looks like you''re trying to do), then you''re calling rand()%3 every time you get that message. If you''re throwing that message often, then it''ll keep changing (the looping effect you describe).

Perhaps I''m just misunderstanding < ... I don''t believe a rand()%3; statement should keep giving random values unless it is declared in a loop. If it did, your computer would freeze and nothing else could happen (couldn''t access any code following it), unless of course this statement creates some sort of thread ... but I highly doubt that.

Like someone said before ... what if you just declared

"int bg = rand()%3;"

outside of all your code at the very beginning (initialization)? Does it have to happen when you get the BACKGROUND_SHADER message? If you do it at the beginning, you can guarantee it''s only declared once, and since it''ll be global, you can use it anywhere.

hope I''m helping <

eriol
The answer, I believe would be very simple.
Try this
change

int bg = rand()%3;
to

static int bg = rand()%3;

that should do it.

Feel free to email me.
[email=JValentine_13@hotmail.com]contact[/email]

This topic is closed to new replies.

Advertisement