Shop OBEX P1 Docs P2 Docs Learn Events
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: 18,069

    If you are using spin, check the RND instruction.

  • AribaAriba Posts: 2,690

    Something like that:

    VAR
      long random, x
    
    
    PUB
      random := CNT                 'initial seed
      repeat
        ? random                    'next random number
        x := (random & 4095) + 1    'limit to range
        ...
    
  • 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: 5,051
    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.