Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

randint()

Get a random integer from a closed interval.

(1) randint [different seed every time]
import random

for _ in range(5):
    print(random.randint(1, 3))
import std/random

randomize()  # !!! call it !!!

for _ in 1..5:
  echo rand(1..3)

Possible output:

3
1
3
1
2

Every time you execute these programs, they'll produce different numbers. But the numbers fall in the [1, 3] closed interval.

Python initializes the random number generator with a different seed every time you launch your program.

Nim doesn't do this implicitly. To get this result, you must call randomize() explicitly.

Warning! If you forget to call randomize(), then your Nim program will produce the same random numbers in the same order. In this case, the random number generator will be initialized implicitly with a default value.

(2) fixing the seed
import random

random.seed(42)  # using a fixed seed

for _ in range(5):
    print(random.randint(1, 3))
import std/random

randomize(77)  # using a fixed seed

for _ in 1..5:
  echo rand(1..3)

𝥶Now, if you execute the program, it'll produce the same random numbers in the same order. What is it good for? To be able to reproduce an experiment, for instance.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 08, 19:42