needmoreeasy EN 한국어
Learn

12 — Random: dice and picks

★★☆☆☆ (2/5)random
Prerequisites
11 — Break
You will end up with
a program that rolls a die and picks a color

A game needs a surprise. Rolling a die, or picking one of several things, is a single sentence. There is nothing to load first and nothing to switch on.

Steps

  1. Type this into the writing box:

    set die to random number from 1 to 6
    show die
    
    run it →

    Run it a few times. Any number from 1 to 6 comes out. Korean is 주사위는 1부터 6까지 무작위 숫자.

  2. Picking one of the things you name:

    set color to pick from red or green or blue
    show color
    
    run it →

    Korean is 색은 빨강 또는 초록 또는 파랑 중에서 골라.

  3. Mix it with what you already know and it is a game:

    set answer to random number from 1 to 3
    ask number guess Pick 1, 2 or 3
    if guess equals answer
        show Correct!
    else
        show Not this time
        show answer
    end
    
    run it →
  4. Roll several times and keep them:

    set rolls to list of
    repeat 3 times
    set one to random number from 1 to 6
    append one to rolls
    end
    show rolls
    
    run it →
  5. (Skip this for now.) In beginner syntax the same jobs have helper names. You load them with use random and then write random_number(1, 6). Written as sentences, that line is not needed.

Try it yourself

Roll the die twice and show both:

set first to random number from 1 to 6
set second to random number from 1 to 6
show first
show second
run it →

What you learned

  • random number from 1 to 6 / 1부터 6까지 무작위 숫자 rolls a die.
  • pick from red or green or blue / ... 중에서 골라 picks one of several.
  • Written as sentences, nothing has to be loaded first.
  • Chance mixes straight into conditions, loops and lists — mix them and you have a game.
  • Beginner syntax calls the same jobs by name, after use random. That comes later, in 61 — Modules.

This page on GitHub