Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy4 random number method (solved)

    Scheduled Pinned Locked Moved LoPy
    lopy4randomexamplecodeforum
    6 Posts 3 Posters 1.3k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      PyBuggz
      last edited by PyBuggz

      Hi, just looking for the library/function to generate a random number for three choices.

      like
      import random

      complains about no module.
      I've tried urandom, os.random,
      pyb as per this post
      https://forum.micropython.org/viewtopic.php?t=2727

      TIA Dave

      robert-hhR danD 2 Replies Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @PyBuggz
        last edited by robert-hh

        @pybuggz A search for "random" in the documentation reveals that link:
        https://docs.pycom.io/firmwareapi/micropython/ucrypto#methods

        1 Reply Last reply Reply Quote 0
        • danD Offline
          dan @PyBuggz
          last edited by

          @pybuggz have you tried uos.urandom(x)? http://docs.micropython.org/en/latest/library/uos.html#uos.urandom

          P 1 Reply Last reply Reply Quote 0
          • P Offline
            PyBuggz @dan
            last edited by

            @dan
            TNX, Looks like what I want..(note no 'u', just 'os')

            os.urandom(3) gives results like

            >>> os.urandom(3)
            b'\x99\xcc\xe6'
            

            OK
            os.urandom(1) gives me a single byte, and this is how to make it an integer.
            I've tried:

            >>> print(int.from_bytes(os.urandom(1),"big"))
            60
            

            I want something between 0 and 3, so I need to limit the range somehow..

            Also, how do I know which Micropython functions are in 'pycompython'?

            regards
            Dave

            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @PyBuggz
              last edited by

              @pybuggz said in LoPy4 random number method:

              n = os.urandom(1)[0]
              

              also results in a number of 0-255. To get a number of 0-3, just use n % 4 or n & 0x3. If the range you need is not a power of 2, then start with a larger random number to reduce non-randomness of the result.
              What do you measn with pycompython? if you waht to know the methods of a module, import that module and call dir(module_name), e.g.

              import uos
              dir(uos)
              

              P.S.: uos is the 'Micro' version of os, and MicroPython provides os as the alias to uos.

              P 1 Reply Last reply Reply Quote 1
              • P Offline
                PyBuggz @robert-hh
                last edited by

                @robert-hh Thanks

                What I want here is to simulate some users doing 1 of 3 things, so:

                >>> print(uos.urandom(1)[0] %3)
                0
                >>> print(uos.urandom(1)[0] %3)
                0
                >>> print(uos.urandom(1)[0] %3)
                1
                >>>
                >>> print(uos.urandom(1)[0] %3)
                2
                >>> print(uos.urandom(1)[0] %3)
                2
                

                simplifies my code and will be OK.

                Regarding Micropython, I've 'hacked' a bit of other code at times and this is the first time I'm having to find the functions I need, so little things like 'dir(os)' are gems.

                Dave

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom