Shop OBEX P1 Docs P2 Docs Learn Events
Random number — Parallax Forums

Random number

LightfootLightfoot Posts: 228
edited 2007-08-06 01:51 in General Discussion
How to you generate a RANDOM value with a maximum of 200?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • BeanBean Posts: 8,129
    edited 2007-07-28 13:04
    One way is the just generate another random number if the value is over 200

    DO
    · RANDOM tempByte
    UNTIL temp <= 200

    Or you could scale the value using WORD variables

    RANDOM tempWord
    tempWord = tempWord / 327

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • LightfootLightfoot Posts: 228
    edited 2007-08-06 01:45
    now I am trying to generate a number between 3000 and 7000. I tried the diving trick but I do not think it lets you divide by fractions. IS there any other way?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-08-06 01:51
    You can do it like this:

    RANDOM result
    result = result // 4001
    result = result + 3000
    



    This works because // returns a value from 0 to the divisor minus one. The span between the two values is 4000. Your desired minimum value is 3000, hence gets added to the result of the modulus operation.
Sign In or Register to comment.