[Microbit-Python] Micropython random numbers

Mark Shannon mark at hotpy.org
Wed Nov 25 15:25:03 EST 2015



On 25/11/15 14:40, David Whale wrote:
> There is indeed microbit.random()
>
>
> Note that there is some evidence of a bug in the runtime random function
> at the moment when I use it from Touch Develop, it's not very random,
> because it is not seeded (I think).
>
> My Dr Who Daleks game always shoots me on the first two goes, because
> the random number sequence is, I think, the same every time.
>
> It would be good if the first time random() was called it seeded it with
> the running_time variable so that it was automatically randomized.
Except that running_time is initialized to 0 at start up, so you will 
still predictable values.

I would suggest getting seed bits from the accelerometer.
 >>> a.get_x() ^ a.get_y() ^ a.get_z()
should do the job.

Cheers,
Mark.

>
> David.
>
> ___________________________________________________________
> David Whale, B.Sc (Hons), MIET
> *Software Engineer and IET Schools Liaison Officer, Essex*
>
> email: dwhale at theiet.org <mailto:dwhale at theiet.org>
> twitter: @whaleygeek
> blog: blog.whaleygeek.co.uk <http://blog.whaleygeek.co.uk>
>
> Co-author of the new book "Adventures in Minecraft"
> <http://amzn.to/ZGfxZG> - lets get kids coding!
>
>
> On 25 November 2015 at 14:15, Damien George <damien.p.george at gmail.com
> <mailto:damien.p.george at gmail.com>> wrote:
>
>     There is a microbit.random() function.
>
>     On Wed, Nov 25, 2015 at 2:10 PM, Michael <sparks.m at gmail.com
>     <mailto:sparks.m at gmail.com>> wrote:
>      > Check one of the sensors to see which one is changing lots, and
>     take some
>      > digits from that?
>      >
>      > (If you're shaking it and take values from the accelerometer with
>     pauses
>      > between from the compass you'll probably get values as random as
>     shaking a
>      > dice :-) )
>      >
>      >
>      > Michael.
>      >
>      > On 25 November 2015 at 13:50, Martin O'Hanlon
>     <martin at ohanlonweb.com <mailto:martin at ohanlonweb.com>> wrote:
>      >>
>      >> A microbit is on its way to me...  I've got an idea but it would
>     be really
>      >> useful to be able to create a random number, the equivalent of
>      >> random.randint(0,5).
>      >>
>      >> From a quick look through Micropython's docs I dont think a random
>      >> function is available.
>      >>
>      >> Has anyone got any inspired thoughts about how I might create a
>     'pseudo
>      >> random number' using micropython.
>      >>
>      >> Mart
>      >>
>      >>
>      >> --
>      >>
>      >> Blog - www.stuffaboutcode.com <http://www.stuffaboutcode.com>
>      >>
>      >> Twitter - @martinohanlon
>      >>
>      >>
>      >> _______________________________________________
>      >> Microbit mailing list
>      >> Microbit at python.org <mailto:Microbit at python.org>
>      >> https://mail.python.org/mailman/listinfo/microbit
>      >>
>      >
>      >
>      > _______________________________________________
>      > Microbit mailing list
>      > Microbit at python.org <mailto:Microbit at python.org>
>      > https://mail.python.org/mailman/listinfo/microbit
>      >
>     _______________________________________________
>     Microbit mailing list
>     Microbit at python.org <mailto:Microbit at python.org>
>     https://mail.python.org/mailman/listinfo/microbit
>
>
>
>
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>


More information about the Microbit mailing list