[Implemented] Random

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

[Implemented] Random

Post by Polo »

I've made the request before, but could the function Random have two parameters ? I just don't see why values should specificaly start from 0, we need the ability to choose the start value :)
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

What about this: Random(10)+StartValue.

May be it should be written in the doc ;)
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Code: Select all

Random(15)+5
It will now start at 5 and end at 20...
I like logic, hence I dislike humans but love computers.
Gansta93
Enthusiast
Enthusiast
Posts: 238
Joined: Wed Oct 20, 2004 7:16 pm
Location: The Village
Contact:

Post by Gansta93 »

Why don't you want to add it?
Be seeing you! :-)

Gansta93
If you speak french, you can visite Le Monde de Gansta93 (Gansta93's World)
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's just useless.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Far from useless.
It makes the command a little bit more advanced without too much work...
It's good to have workaround, but too much workaround kill easy programmation ;)
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

hmm... i agree. it would be nice to have this one. cause its not hard to implement that "brand new feature". its only a matter of few seconds(?!) i guess. :D

Code: Select all

Procedure MyRandom(Min.l, Max.l)
  Result.l=Random(Max-Min)+Min
  ProcedureReturn Result
EndProcedure

MessageRequester("your lucky number is... ;)", Str(MyRandom(10, 20)))
p.s.: there is almost always a workaround, for almost everything, but still... :cry:

c ya,
nco2k
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Currently the command take 1 argument, which is max. So adding min as optional sounds wierd..
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I also want things to be easy, but I agree with Fred on this.
The random function is like this in every language(i think), why should it be different in PB.
Min-Max on random sounds quite stupid... (my personal feeling)
Last edited by Joakim Christiansen on Sat May 21, 2005 2:36 am, edited 1 time in total.
I like logic, hence I dislike humans but love computers.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

pretty numb, but what the heck...

Code: Select all

Procedure.l x_random(low.l,high.l)                                  ; generates a random int in the range low to high
  ;
  ; *** return a random var from and including 'low.l' to and including 'high.l'
  ;
  ; (i find the pb random function somewhat confusing now and again...)
  ;
  ProcedureReturn low+Random(high-low)
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Yea most randoms only return a float/double from 0 to 0.99999...

So you'd need to do rand()*10 to get an integer from 0 to 9 and add whatever constant you need for max/min, etc. Already easy enough. No change is my vote.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

it works, i'd rather add a rnd() for floats to emulate other languages than change the current one
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

nco2k wrote:hmm... i agree. it would be nice to have this one. cause its not hard to implement that "brand new feature". its only a matter of few seconds(?!) i guess. :D

Code: Select all

Procedure MyRandom(Min.l, Max.l)
  Result.l=Random(Max-Min)+Min
  ProcedureReturn Result
EndProcedure

MessageRequester("your lucky number is... ;)", Str(MyRandom(10, 20)))
p.s.: there is almost always a workaround, for almost everything, but still... :cry:

c ya,
nco2k
Here is what I want ;)
And also, nobody is speaking of optional parameter ;)
Gansta93
Enthusiast
Enthusiast
Posts: 238
Joined: Wed Oct 20, 2004 7:16 pm
Location: The Village
Contact:

Post by Gansta93 »

Joakim Christiansen wrote:I also want things to be easy, but I agree with Fred on this.
The random function is like this in every language(i think), why should it be different in PB.
Min-Max on random sounds quite stupid... (my personal feeling)
No, there are language where the Minimum parameter exists. And I agree with others, it would be useful.
If I remember well, in PHP and MIrc there is the parameter LowValue. I don't know for C, C++ etc.
Be seeing you! :-)

Gansta93
If you speak french, you can visite Le Monde de Gansta93 (Gansta93's World)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> nobody is speaking of optional parameter

It's not needed. Some things just need to be done by the programmer. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply