needmoreeasy EN 한국어
Learn

10 — Random: dice and picks

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

Games need surprises. NME bundles a random helper that Python provides, loaded with one use line.

Steps

  1. Create dice.nme:

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

    Run it a few times: die is a random whole number from 1 to 6.

  2. Picking one of several choices works the same way:

    use random latest
    set color to pick from red or green or blue
    show color
    
    run it →
  3. Korean loads the helper with 랜덤 사용 최신 and uses sentence endings:

    랜덤 사용 최신
    주사위는 1부터 6까지 랜덤정수
    주사위 보여줘
    색은 빨강 또는 초록 또는 파랑 중에서 랜덤선택
    색 보여줘
    
    run it →
  4. The helper names work in expressions too: 랜덤정수(a, b) is a random number and 랜덤선택(values) picks from a list.

    use random latest
    set roll to random number from 1 to 6
    show roll
    
    run it →

Try it yourself

Roll two dice and show both:

use random latest
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

  • use random latest / 랜덤 사용 최신 loads the bundled random helper.
  • random number from 1 to 6 / 1부터 6까지 랜덤정수 roll a die.
  • pick from red or green or blue / ... 중에서 랜덤선택 pick one choice.
  • use file may join use random in the same program — guide 15 saves its best score that way.

This page on GitHub