locked
Generating random number < 1 RRS feed

  • Question

  • I'd like some advice on how to generate a random number in Small Basic that is less than 1 e.g. 0.955

    Have tried A = Math.GetRandomNumber(5)

    B = A * 0.333

    TextWindow.Write(B)

    Outcome is that the numbers do not seems that random and tend to be 0.333, 0.666 etc

    Tuesday, June 11, 2013 9:05 AM

Answers

  • Hi Franganistan

    Instead of multiplying by a decimal to convert to a number < 1, generate a random number up to 999 and then divide this by 1000. This will generate a decimal between 0.001 and 0.999.

    While "true"
      rnum = Math.GetRandomNumber(999)/1000
      TextWindow.WriteLine(rnum)
      Program.Delay(100)
    EndWhile

    • Marked as answer by Franganistan Tuesday, June 11, 2013 10:15 AM
    Tuesday, June 11, 2013 10:01 AM

All replies

  • Hi Franganistan

    Instead of multiplying by a decimal to convert to a number < 1, generate a random number up to 999 and then divide this by 1000. This will generate a decimal between 0.001 and 0.999.

    While "true"
      rnum = Math.GetRandomNumber(999)/1000
      TextWindow.WriteLine(rnum)
      Program.Delay(100)
    EndWhile

    • Marked as answer by Franganistan Tuesday, June 11, 2013 10:15 AM
    Tuesday, June 11, 2013 10:01 AM
  • Jibba Jabba,

    Thank you - worked perfectly.

    Tuesday, June 11, 2013 10:15 AM
  • Cheers. And happy coding.
    Tuesday, June 11, 2013 10:19 AM