Shop Learn
Any very basic way to randomly select a number between 1 and 4096 with spin — Parallax Forums

Any very basic way to randomly select a number between 1 and 4096 with spin

What i am looking for is a very basic number generator that returns a number between 1 and 4096

Does not need to be very random, just the most simple way to get some numbers, even if they come up multiple times or consecutively.

Comments

  • Cluso99Cluso99 Posts: 17,955

    If you are using spin, check the RND instruction.

  • AribaAriba Posts: 2,504

    Something like that:

    VAR
      long random, x
    
    
    PUB
      random := CNT                 'initial seed
      repeat
        ? random                    'next random number
        x := (random & 4095) + 1    'limit to range
        ...
    
  • GenetixGenetix Posts: 1,542

    Ariba,

    Brilliant idea of using CNT.

    Reminds me of the Apple II using the time between keyboard presses as a Random Number.

  • Wuerfel_21Wuerfel_21 Posts: 1,870
    edited 2021-09-14 20:50

    CNT at the beginning of the program is not a good source of randomness. If you're loading over serial, it will have a semi-random value, but when loading from EEPROM, it is always(?) the same

  • @Ariba said:
    Something like that:

    VAR
      long random, x
      
    
    PUB
      random := CNT                 'initial seed
      repeat
        ? random                    'next random number
        x := (random & 4095) + 1    'limit to range
        ...
    

    Brilliant, thanks a lot.

Sign In or Register to comment.