rnd()

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

rnd()

Post by y offs et »

As I read it "rnd() - returns a random number in the range 0 to 1. "
means -
integers 0 and 1 are possible, as well as fractions between
correct?
User avatar
VilleK
Site Admin
Posts: 2382
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

The rnd()-function use the built-in Random-function in Delphi which behaves just like the corresponding function in most programming languages. It returns values in the range: 0 <= Number < 1.0

So it can be zero and 0.99999 but never 1.0. If you type: "int x=rnd() * 5;" you will get values between 0 and 4.

http://www.delphibasics.co.uk/RTL.asp?Name=Random
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

Thanks for link. I've overviewed at least a dozen languages and there is always little differences. I'll clarify the Docs.

"int x=rnd() * 5;"
Small question - if x is already defined as an integer variable, is the "int" required? I've been using x = round(rnd()*5);
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post by jph_wacheski »

So

"int x=rnd() * 5;" you will get values between 0 and 4.9995

correct?
iterationGAMES.com
User avatar
VilleK
Site Admin
Posts: 2382
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

You don't have to redeclare x if it is already defined so "x=" is enough.

float f=rnd() * 5; //Gives a number between 0 and 4.99999...

int x=round(rnd() * 5); //Gives an integer between 0 and 5

int x=rnd() * 5; //Gives an integer between 0 and 4

Round() rounds values to the nearest integer, so 4.6 rounds up to 5. The default number to integer conversion is to truncate the decimals so that 4.7894 becomes 4.

I'm writing from memory and I haven't verified the information above in the editor, but this is how it should work :)
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

according to the link -

" round(x) - returns the integer part of x " from the Docs is incorrect.

trunc() is the function that produces this

http://www.delphibasics.co.uk/RTL.asp?Name=Trunc

----------------

On checking others, I see you have been inventing some. Hmm... :?
User avatar
VilleK
Site Admin
Posts: 2382
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

If you find inconsistencies between the wiki and the Delphi-docs then the Delphi-docs are probably more correct.
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

OK. So that means there is no actual truncation function and I'll have to find another way.
User avatar
VilleK
Site Admin
Posts: 2382
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

Trunc is basically x-frac(x).
Post Reply