Difference between revisions of "11 Random number generator"

From Banana Pi Wiki
Jump to: navigation, search
(random number)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[File:Webduino_gif.gif|thumb|Overview: [[BPI-Bit]]]]
 +
[[File:Micropython3.png|thumb|[[1 Get the development suite]] ]]
 +
[[File:Micropython2.png|thumb|[[2 Wired connection board]] ]]
 +
[[File:Micropython2.png|thumb|[[3 Try burning the board]] ]]
 +
[[File:Micropython2.png|thumb|[[4 Hello, World!]] ]]
 +
[[File:Micropython2.png|thumb|[[5 Light up all kinds of LED]] ]]
 +
 +
 +
[[File:Micropython4.png|thumb|[[1  Auto series one key burn]] ]]
 +
[[File:Micropython2.png|thumb|[[2  ESPBlocks Beginner's only]] ]]
 +
[[File:Micropython2.png|thumb|[[3 Mpfshell details]] ]]
 +
[[File:Micropython2.png|thumb|[[4 Pycharm IDE programming]] ]]
 +
 +
 +
[[File:Micropython5.png|thumb|[[1. Panel scrolling text]] ]]
 +
[[File:Micropython2.png|thumb|[[2. Panel display images]] ]]
 +
[[File:Micropython2.png|thumb|[[3 Bottom IO port control]] ]]
 +
[[File:Micropython2.png|thumb|[[4 Panel key detection]] ]]
 +
[[File:Micropython2.png|thumb|[[5 Gets the board temperature]] ]]
 +
[[File:Micropython2.png|thumb|[[6 Play MIDI music]] ]]
 +
[[File:Micropython2.png|thumb|[[7 Photosensitive gesture]] ]]
 +
[[File:Micropython2.png|thumb|[[8. MPU-9250 9-axis sensor]] ]]
 +
[[File:Micropython2.png|thumb|[[9. Make a compass]] ]]
 +
[[File:Micropython2.png|thumb|[[10 Free to define gestures]] ]]
 +
[[File:Micropython2.png|thumb|[[11 Random number generator]] ]]
 +
[[File:Micropython2.png|thumb|[[12 S2m Scratch2]] ]]
 +
[[File:Micropython2.png|thumb|[[13 Codelab Scratch3]] ]]
 +
 +
[[File:Micropython6.png|thumb|[[1 The basic algorithm]]  ]]
 +
[[File:Micropython2.png|thumb|[[2 WiFI wireless connection]] ]]
 +
[[File:Micropython2.png|thumb|[[3 WiFI wireless programming]] ]]
 +
[[File:Micropython2.png|thumb|[[MQTT communication applications]]]]
 +
 
=Random number generator=
 
=Random number generator=
  
Line 32: Line 65:
 
  answer = random.randrange(100) + random.random()
 
  answer = random.randrange(100) + random.random()
 
  display.scroll(str(answer))
 
  display.scroll(str(answer))
 +
 +
=Seed random number=
 +
 +
The random number generators that computers use are not really random number generators they're just Numbers that are generated by random seeds so where does that seed number come from?Usually from time or sensor readings, such as built-in timer and a thermometer in the chip
 +
 +
Sometimes you want to have a repeatable random behavior: a repeatable random source For example each dice all need the same random value By setting the seed value is easy to do this Given a known seed, a random number generator will create the same set of random Numbers Seed is set, this version of the dice the program always produce the same results
 +
 +
==sample code==
 +
 +
from microbit import *
 +
import random
 +
random.seed(1337)
 +
while True:
 +
    if button_a.was_pressed():
 +
        display.show(str(random.randint(1, 6)))
 +
 +
Running the above program we always get the same result, showing '5' on the led panel, because the seed we are given here is a fixed value so the program always produces a fixed number

Latest revision as of 03:39, 21 February 2019

Overview: BPI-Bit



Random number generator

randomness

What is randomness? Randomness means unpredictability. Real randomness exists only in the natural world. Where lightning strikes randomly; If a storm is brewing somewhere, you can be pretty sure there will be lightning, but you can't predict exactly where it will be, so don't stand under a tree. For example, the teacher wanted the students to get up to answer the question, but no one raised his hand to answer the question, at this time the teacher shouted a seat number, the seat number is also random.

MicroPython comes with a random number module that makes it easy to introduce random Numbers into your code. For example, here's an example of how to scroll a random name on the screen:

from microbit import *
import random
names = ["Mary", "Yolanda", "Damien", "Alia", "Kushal", "Mei Xiu","Zoltan" ]
display.scroll(random.choice(names))

Lists (names) contain strings of 7 different names. Using the random. Choice method, the list (names) is taken as an argument and the randomly selected items are returned. This item (the name chosen at random) will be a parameter to display.scroll. The effect is that you can see the randomly selected names on the led display panel

random number

Random Numbers are very useful.They're very common in games, like dice.MicroPython provides some useful random number methods.Let's make a simple die. Sample code

from microbit import *
import random
display.show(str(random.randint(1, 6)))

Each time the board executes this function, it displays a number between 1 and 6. Random. Randint () returns an integer (including 6) between the two parameters 1 and 6. Note that since the show function requires a character to display, we use the STR function here to convert the numeric value to a character (for example, we converted 6 to "6" through STR (6)).

If you want a number between 0 and N, use the random. Randrange () function. If you give it a parameter, it returns a random integer, but not the value of the parameter N (unlike random.Random. Random (), this function will only return floating point Numbers between 0.0 and 1.0. So how to generate larger floating point Numbers

sample code

from microbit import *
import random
answer = random.randrange(100) + random.random()
display.scroll(str(answer))

Seed random number

The random number generators that computers use are not really random number generators they're just Numbers that are generated by random seeds so where does that seed number come from?Usually from time or sensor readings, such as built-in timer and a thermometer in the chip

Sometimes you want to have a repeatable random behavior: a repeatable random source For example each dice all need the same random value By setting the seed value is easy to do this Given a known seed, a random number generator will create the same set of random Numbers Seed is set, this version of the dice the program always produce the same results

sample code

from microbit import *
import random
random.seed(1337)
while True:
   if button_a.was_pressed():
       display.show(str(random.randint(1, 6)))

Running the above program we always get the same result, showing '5' on the led panel, because the seed we are given here is a fixed value so the program always produces a fixed number