Author Topic: Generating a random decimal  (Read 2523 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Generating a random decimal
« on: June 26, 2018, 10:22:17 AM »
I'm trying to store a random number between 1 and 5 in a decimal variable (1.234, 4.567, etc). Unfortunately this code only seems to return integer values:
Code: [Select]
Set decimal [~~MyDecimal] value as random from 1 to 5
Write [Blue] 'Random Decimal = {DEC:~~MyDecimal}' to log
The output looks like this:


I've also tried:
Code: [Select]
Set decimal [~~MyDecimal] value as random from 1.1 to 5.1
Write [Blue] 'Random Decimal = {DEC:~~MyDecimal}' to log

...which now appears to be decimal numbers but inherently the range is not what I want (since I'm after 1 to 5 not 1.1 to 5.1).

THIS does work:
Code: [Select]
Set decimal [~~MyDecimal] value to the converted value of {RANDOMDEC:1.000:5.000}
Write [Blue] 'Random Decimal = {DEC:~~MyDecimal}' to log

...and it looks like it will produce the correct range as well as the number of decimal places I'm after.

I don't understand why the first example above doesn't produce random decimals between 1 and 5, and changing the decimal place rounding on the "Set a Decimal Value" interface has no other effects. Any thoughts?

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2827
Re: Generating a random decimal
« Reply #1 on: June 26, 2018, 12:05:07 PM »
Code: [Select]
Set decimal [~~myVar] value as random from 1 to 5
Write '[Blue] {DEC:~~myVar}' to log

When I do this, I get values such as 1.2589369 and 3.0419726 in the log.

Do you have rounding turned on or something?




Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Generating a random decimal
« Reply #2 on: June 26, 2018, 01:30:42 PM »
well....now I have no issue.

not sure what happened but all is well :o