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?
rnd()
Moderator: Moderators
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
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
- jph_wacheski
- Posts: 1005
- Joined: Sat Feb 16, 2008 8:10 pm
- Location: Canada
- Contact:
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
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

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...
" 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...
