GLBasic forum

Main forum => GLBasic - en => Topic started by: Ian Price on 2007-Dec-24

Title: Rand(Min,Max)??
Post by: Ian Price on 2007-Dec-24
I know that GLBasic supports random numbers in the form of RND(Max), but this always returns a value between 0 and the Max value.

Is there a command like RAND(Min, Max), where the command returns a value between (and inclusive of) the two values?

I have only skimmed through the Index, so maybe I missed a similar command. Does it exist, and if not, can it be added?

[EDIT] BTW I created a function to do this, but a built in command could be useful to newcomers.
Title: Rand(Min,Max)??
Post by: Moru on 2007-Dec-24
The normal behaviour of basic is to give a number between 0 and 0.9999. This one you multiply with whatever you want the range to be and then add the number you want to be the start of the range. If you want newcomers to have it easier, post your function for them to use too! That's what it's all about anyway, isn't it?
Title: Rand(Min,Max)??
Post by: Kitty Hello on 2007-Dec-24
Hm... I though this RND would be theeasy to use:
For RND(5,7) I use:
5+RND(7-5)
Title: Rand(Min,Max)??
Post by: Schranz0r on 2007-Dec-24
Hmm Gernot i think RND(0,9) for example, could be better then RND(9).
So you can do RND(5,10),  5 MIN 10 MAX....
Should be easier to use...

@ iprice

you can use that:

negativ and positiv values!

ex.: RAND(-10,10) or RAND(10,20) or something like ;)

Code (glbasic) Select
FUNCTION RAND: minimum, maximum

RETURN minimum + RND(-minimum + maximum)

ENDFUNCTION
Title: Rand(Min,Max)??
Post by: Ian Price on 2007-Dec-24
Quote from: Schranz0rHmm Gernot i think RND(0,9) for example, could be better then RND(9).
So you can do RND(5,10),  5 MIN 10 MAX....
Should be easier to use...

@ iprice

you can use that:

negativ and positiv values!

ex.: RAND(-10,10) or RAND(10,20) or something like ;)

Code (glbasic) Select
FUNCTION RAND: minimum, maximum

RETURN minimum + RND(-minimum + maximum)

ENDFUNCTION
That's very similar to the function I came up with :)
Title: Rand(Min,Max)??
Post by: Moru on 2007-Dec-25
Why is it every new Basic dialect has to come up with it's own way of using RND? Why not stay with the 0 - 0.9999 and leave the programmer to use it as he sees fit? If you want negative values you just do RND() * 20 - 10 and you have a range between -10 and +10.
Title: Rand(Min,Max)??
Post by: Neurox on 2007-Dec-27
Quote from: MoruWhy not stay with the 0 - 0.9999 and leave the programmer to use it as he sees fit?
Sorry, but I prefer the GLBasic way!
It's very comfortable ;-)

Neurox
Title: Rand(Min,Max)??
Post by: Schranz0r on 2007-Dec-27
Quote from: Neurox
Quote from: MoruWhy not stay with the 0 - 0.9999 and leave the programmer to use it as he sees fit?
Sorry, but I prefer the GLBasic way!
It's very comfortable ;-)

Neurox
;) GLBasic rockz ;)