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

    Random number generator

    Scheduled Pinned Locked Moved MicroPython
    randomlopy
    4 Posts 2 Posters 2.3k Views 2 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.
    • T Offline
      timeb
      last edited by

      I had this idea of creating a finite state machine which makes transitions between three states. So, I needed to generate random numbers between 0 and 1. Is there such module for LoPy. I used randint(0,1) but it doesn't work. Any suggestions?

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @timeb
        last edited by livius

        @timeb
        you can try something like this
        https://docs.pycom.io/pycom_esp32/library/ucrypto.html?highlight=getrandbits#ucrypto.crypto.getrandbits

        please be patient that i write this from memory without any test - you should fix errors
        this is only concept

        def Random():
           r = crypto.getrandbits(32)
           return ((r[0]<<24)+(r[1]<<16)+(r[2]<<8)+r[3])/4294967295.0
        

        think about people first, then computers

        1 Reply Last reply Reply Quote 0
        • L Offline
          livius
          last edited by livius

          i see that code work on my Wipy2 without any modification :)
          below results

          Random()
          0.8462145
          >>> Random()
          0.4143322
          >>> Random()
          0.1052132
          >>> Random()
          0.7745724
          >>> Random()
          0.4098172
          >>> Random()
          0.2059813
          >>> Random()
          0.3866896
          

          and bigger test:

          sum=0
          >>> ile=0
          >>> for i in range(1,1000000):
          ...     sum+=Random()
          ...     ile+=1
          ...
          >>> sum
          499948.5
          >>> ile
          999999
          >>> sum/ile
          0.499949
          

          realy near to 0.5 - as you can see it is ok :)

          think about people first, then computers

          T 1 Reply Last reply Reply Quote 0
          • T Offline
            timeb @livius
            last edited by

            @livius Thanks I tried it and it works. I had another question if you could give some suggestion. I have been trying to take the last four bytes of the mac address as source and destination address to send data from one Lopy to a second LoPy. I created a package header as the following:

            _LORA_PKG_FORMAT = "LL%ds"
            _LORA_ACK_FORMAT = "LLB"
            SRC_MAC_ADDR = ubinascii.hexlify(lora.mac().strip()[-4:])

            Destination address
            DEST_MAC_ADDR = b'9cc15603'

            ** pkg = struct.pack(_LORA_PKG_FORMAT, DEST_MAC_ADDR, SRC_MAC_ADDR, msg)

            But when I try running it complains saying ValueError: unsupported format for the line with **. what do you think I am doing wrong?

            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