random() and negative numbers...
Displaying 1-12 of 12 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Technetium

I have some numbers which are calculated from linear equations and used as min/max in the random() function... However, the "x" value in the equations can be positive or negative (both equations are dependant on the same variable, but have different slopes), thus it is very easy for the max value to end up smaller than the the min value. I just tried running it with random(65,0-20) and it spit back an enormous positive number. Weird, I was expecting it would either work or crash the program.

Is it possible that at some point random() could simply take two numbers, in any order, rather than require the first to be the smaller (or equal) one? That would be really awesome.

Posted on 2004-08-26 04:15:51

blues_zodiakos

My brain hurts today, so I don't really follow all of that, but I don't know if random() actually works with negative numbers well. The reason for the big positive number is integer wraparound. If you want a random number to be possibly negative, you can do it in two steps -

i = Random(blah, blah);
a = Random(0,1);
if (a=1) then i -= i*2;

In this way, if a = 1, then it will make i a negative number instead. A 50% chance. It all works out the same, I think.
I'm not sure about the math, as I said, my brain hurts.

Posted on 2004-08-26 04:22:32

Sungam

Easy work-around:

if(min<max)
blah = random(min,max);
else
blah = random(max,min);

Posted on 2004-08-26 04:51:59 (last edited on 2004-08-26 04:54:01)

Technetium

Quote:Originally posted by Sungam

Easy work-around:


It's actually pretty close to what I am doing. I can do that. What I do is actually more like:
if(x>0)
y=random(long function #1 based on x, long function #2 based on x)
else
y=random(long function #2 based on x, long function #1 based on x)

The thing is, this is run pretty close to every screen update, and any if/else I can cut out would help speed things up. That's why I was asking if it is possible/likely that it might take the values in both orders at some point in the future.

Posted on 2004-08-26 05:52:26 (last edited on 2004-08-26 05:53:10)

Toen

Well, if you're going to be running that random statement with the long functions anyway, throwing the if/else in there (especially when it is something simple like x > 0) is probably not going to have any appreciable speed hit

Posted on 2004-08-26 05:58:01

Technetium

Ah. Alrighty then!

Posted on 2004-08-26 14:14:17

mcgrue

You kids all worry about speed and memory too much!

I'm used to working in java. vc is plenty fast. optimization should only come into your thoughts *after* you've created something!

Once you've actually coded up a game, you test it on various platforms, and if parts of it run sluggish on a target machine, *then* you recode the problem spot for speed. Worrying about speed before you need to is just silly and will kill your momentum in actually finishing your project.

Just a friendly bit of advice.

Posted on 2004-08-26 14:16:50

rpgking

Quote:Originally posted by mcgrue


Once you've actually coded up a game, you test it on various platforms, and if parts of it run sluggish on a target machine, *then* you recode the problem spot for speed. Worrying about speed before you need to is just silly and will kill your momentum in actually finishing your project.


I completely agree with you on that one. Worrying about speed beforehand will keep a project in the planning stages for all eternity. The main point of optimization is to optimize code that is already written :P

As far as worrying about memory goes, at one point I have 5 copies of the VSP saved in memory :D

Posted on 2004-08-26 17:42:00

zonker6666

getting a random number between -20 to 60

have u thought of doing Random(0,80)-20; ?

Posted on 2004-08-26 23:28:31

Gayo

That's a bit of a pain if the limits are determined at runtime, though.

Posted on 2004-08-27 02:41:14

zonker6666

hmmmmm

int RandomRange(int n1, int n2)
{

if(n1>n2)
return random(0, n1-n2)+n2;
else
return random(0, n2-n1)+n1;

}


im not 100% sure but i think it works alright -- higher number on either side

Posted on 2004-08-27 08:55:07

zonker6666

ah ya it works

Posted on 2004-08-27 09:00:58


Displaying 1-12 of 12 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.