how can i randomize float value ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 637
Joined: Fri Dec 04, 2015 9:26 pm

how can i randomize float value ?

Post by skinkairewalker »

hello , how can i randomize float or decimal value ?

example : 1.36 , 2.65
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: how can i randomize float value ?

Post by infratec »

Code: Select all

Debug Random(999, 100) / 100.0
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: how can i randomize float value ?

Post by infratec »

If you really want excatly 2 decimals:

Code: Select all

Debug StrD(Random(999, 100) / 100.0, 2)
But only as string :wink:
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: how can i randomize float value ?

Post by STARGÅTE »

Code: Select all

Procedure.f RandomFloat(Maximum.f = 1.0, Minimum.f=0.0)
  
  ProcedureReturn (Maximum-Minimum) * 4.6566128752457969241e-10 * Random(2147483647) + Minimum
  
EndProcedure

Code: Select all

RandomSeed(5)
Debug RandomFloat()
Debug RandomFloat(100.0)
Debug RandomFloat(1, -1)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply