2015-11-09 08:01:19 -08:00
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "Content-type" content = "text/html;charset=UTF-8" / >
< title > Random Class – Wren< / title >
2020-06-05 22:25:23 +00:00
< script type = "application/javascript" src = "../../prism.js" data-manual > < / script >
< script type = "application/javascript" src = "../../wren.js" > < / script >
< link rel = "stylesheet" type = "text/css" href = "../../prism.css" / >
2015-11-09 08:01:19 -08:00
< link rel = "stylesheet" type = "text/css" href = "../../style.css" / >
< link href = '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel = 'stylesheet' type = 'text/css' >
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
the viewport. -->
< meta name = "viewport" content = "width=device-width, initial-scale=1, maximum-scale=1" / >
< / head >
< body id = "top" class = "module" >
< header >
< div class = "page" >
< div class = "main-column" >
< h1 > < a href = "../../" > wren< / a > < / h1 >
< h2 > a classy little scripting language< / h2 >
< / div >
< / div >
< / header >
< div class = "page" >
< nav class = "big" >
2020-06-05 22:25:23 +00:00
< a href = "../../" > < img src = "../../wren.svg" class = "logo" > < / a >
2015-11-09 08:01:19 -08:00
< ul >
2020-06-05 22:25:23 +00:00
< li > < a href = "../" > Back to Modules< / a > < / li >
< li > < a href = "./" > random module< / a > < / li >
2015-11-09 08:01:19 -08:00
< / ul >
< section >
< h2 > random classes< / h2 >
< ul >
< li > < a href = "random.html" > Random< / a > < / li >
< / ul >
< / section >
< / nav >
< nav class = "small" >
< table >
< tr >
< td > < a href = "../" > Modules< / a > < / td >
< td > < a href = "./" > random< / a > < / td >
< / tr >
< tr >
< td colspan = "2" > < h2 > random classes< / h2 > < / td >
< / tr >
< tr >
< td >
< ul >
< li > < a href = "random.html" > Random< / a > < / li >
< / ul >
< / td >
< td >
< ul >
< / ul >
< / td >
< / tr >
< / table >
< / nav >
< main >
< h1 > Random Class< / h1 >
2020-06-05 22:25:23 +00:00
< p > A simple, fast pseudo-random number generator. Internally, it uses the < a href = "https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear" > well
equidistributed long-period linear PRNG< / a > (WELL512a).< / p >
< p > Each instance of the class generates a sequence of randomly distributed numbers
based on the internal state of the object. The state is initialized from a
< em > seed< / em > . Two instances with the same seed generate the exact same sequence of
numbers.< / p >
2020-08-29 19:07:45 +00:00
< p > It must be imported from the < a href = "../" > random< / a > module:< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
import "random" for Random
2020-08-29 19:07:45 +00:00
< / pre >
2020-06-05 22:25:23 +00:00
2020-08-29 19:07:45 +00:00
< h2 > Constructors < a href = "#constructors" name = "constructors" class = "header-anchor" > #< / a > < / h2 >
< h3 > Random.< strong > new< / strong > () < a href = "#random.new()" name = "random.new()" class = "header-anchor" > #< / a > < / h3 >
< p > Creates a new generator whose state is seeded based on the current time.< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new()
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > Random.< strong > new< / strong > (seed) < a href = "#random.new(seed)" name = "random.new(seed)" class = "header-anchor" > #< / a > < / h3 >
< p > Creates a new generator initialized with [seed]. The seed can either be a
2020-06-05 22:25:23 +00:00
number, or a non-empty sequence of numbers. If the sequnce has more than 16
elements, only the first 16 are used. If it has fewer, the elements are cycled
2020-08-29 19:07:45 +00:00
to generate 16 seed values.< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
Random.new(12345)
Random.new("appleseed".codePoints)
< / pre >
2020-08-29 19:07:45 +00:00
< h2 > Methods < a href = "#methods" name = "methods" class = "header-anchor" > #< / a > < / h2 >
< h3 > < strong > float< / strong > () < a href = "#float()" name = "float()" class = "header-anchor" > #< / a > < / h3 >
< p > Returns a floating point value between 0.0 and 1.0, including 0.0, but excluding
1.0.< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
System.print(random.float()) //> 0.53178795980617
System.print(random.float()) //> 0.20180515043262
System.print(random.float()) //> 0.43371948658705
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > < strong > float< / strong > (end) < a href = "#float(end)" name = "float(end)" class = "header-anchor" > #< / a > < / h3 >
< p > Returns a floating point value between 0.0 and < code > end< / code > , including 0.0 but
excluding < code > end< / code > .< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
System.print(random.float(0)) //> 0
System.print(random.float(100)) //> 20.180515043262
System.print(random.float(-100)) //> -43.371948658705
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > < strong > float< / strong > (start, end) < a href = "#float(start,-end)" name = "float(start,-end)" class = "header-anchor" > #< / a > < / h3 >
< p > Returns a floating point value between < code > start< / code > and < code > end< / code > , including < code > start< / code > but
excluding < code > end< / code > .< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
System.print(random.float(3, 4)) //> 3.5317879598062
System.print(random.float(-10, 10)) //> -5.9638969913476
System.print(random.float(-4, 2)) //> -1.3976830804777
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > < strong > int< / strong > (end) < a href = "#int(end)" name = "int(end)" class = "header-anchor" > #< / a > < / h3 >
< p > Returns an integer between 0 and < code > end< / code > , including 0 but excluding < code > end< / code > .< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
System.print(random.int(1)) //> 0
System.print(random.int(10)) //> 2
System.print(random.int(-50)) //> -22
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > < strong > int< / strong > (start, end) < a href = "#int(start,-end)" name = "int(start,-end)" class = "header-anchor" > #< / a > < / h3 >
< p > Returns an integer between < code > start< / code > and < code > end< / code > , including < code > start< / code > but excluding
< code > end< / code > .< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
System.print(random.int(3, 4)) //> 3
System.print(random.int(-10, 10)) //> -6
System.print(random.int(-4, 2)) //> -2
< / pre >
2020-08-29 19:07:45 +00:00
< h3 > < strong > sample< / strong > (list) < a href = "#sample(list)" name = "sample(list)" class = "header-anchor" > #< / a > < / h3 >
< p > Selects a random element from < code > list< / code > .< / p >
< h3 > < strong > sample< / strong > (list, count) < a href = "#sample(list,-count)" name = "sample(list,-count)" class = "header-anchor" > #< / a > < / h3 >
< p > Samples < code > count< / code > randomly chosen unique elements from < code > list< / code > .< / p >
< p > This uses “ random without replacement” sampling— no index in the list will
be selected more than once.< / p >
< p > Returns a new list of the selected elements.< / p >
< p > It is an error if < code > count< / code > is greater than the number of elements in the list.< / p >
< h3 > < strong > shuffle< / strong > (list) < a href = "#shuffle(list)" name = "shuffle(list)" class = "header-anchor" > #< / a > < / h3 >
< p > Randomly shuffles the elements in < code > list< / code > . The items are randomly re-ordered in
place.< / p >
2020-06-05 22:25:23 +00:00
< pre class = "snippet" >
var random = Random.new(12345)
var list = (1..5).toList
random.shuffle(list)
System.print(list) //> [3, 2, 4, 1, 5]
< / pre >
2020-08-29 19:07:45 +00:00
< p > Uses the Fisher-Yates algorithm to ensure that all permutations are chosen
with equal probability.< / p >
< p > Keep in mind that a list with even a modestly large number of elements has an
2020-06-05 22:25:23 +00:00
astronomically large number of permutations. For example, there are about 10^74
2020-08-29 19:07:45 +00:00
ways a deck of 56 cards can be shuffled. The random number generator’ s internal
2020-06-05 22:25:23 +00:00
state is not that large, which means there are many permutations it will never
2020-08-29 19:07:45 +00:00
generate.< / p >
2015-11-09 08:01:19 -08:00
< / main >
< / div >
< footer >
< div class = "page" >
< div class = "main-column" >
< p > Wren lives
2019-02-06 02:52:27 +00:00
< a href = "https://github.com/wren-lang/wren" > on GitHub< / a >
2015-11-09 08:01:19 -08:00
— Made with ❤ by
< a href = "http://journal.stuffwithstuff.com/" > Bob Nystrom< / a > and
2020-06-12 17:15:45 +00:00
< a href = "https://github.com/wren-lang/wren/blob/main/AUTHORS" > friends< / a > .
2015-11-09 08:01:19 -08:00
< / p >
< div class = "main-column" >
< / div >
< / footer >
< / body >
< / html >