# Vocabulary/query

>>
<< ` `
Down to: Dyad ` `
Back to: Vocabulary
Thru to: Dictionary

`? y`Roll

Rank 0 *-- operates on individual atoms of y, producing a result of the same shape --*
WHY IS THIS IMPORTANT?

Generates a random number uniformly distributed in a range determined by integer `y`.

`y`**range of random number**`0`floating-point value in interval `(0,1)``1``0`(always)`2`Boolean `>1`integer or extended integer from list `i.y`

? 0 NB. result will be 0 to 1 0.242499 ? 0 0.80461 ? 100 NB. result will be 0 to 99 30 ? 100 86 ? 10 # 100 91 93 17 97 58 51 16 75 42 40

### Common uses

1. Make a random boolean matrix

? 5 5 $ 2 1 0 1 0 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0

2. Toss a biased coin 10 times (1=Heads / 10% chance, 0=Tails / 90% chance)

0.1 > ?20#0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0

3. Pick a letter at random

(?26) { 'abcdefghijklmnopqrstuvwxyz' f

### Related Primitives

Roll (Fixed seed) (`?. y`)

### Details

1. J offers a choice of random-number generator (RNG):

**Code****RNG Name****Time**1 GB_Flip 1 2 Mersenne Twister (default) 1 3 DX-1597-4d 3 4 MRG32k3a 8 0 combination of all

the above RNGs

12

Select the RNG with `9!:43 n` where `n` comes from the table above, or use `9!:42 ''` to see which RNG is selected.

2. The sequence of random numbers depends on the *state* of the RNGs.
This state, which includes the choice of RNG to use, can be read by

RNGstate =: 9!:44 ''

and restored by `9!:45 RNGstate`.
The state is a list of boxes whose format should not be disturbed.

3. The state of the RNG can be *reset* by

9!:1 initvalue

where `initvalue` is an integer atom
(or, for the Mersenne Twister generator only, an integer list) that selects a starting state.
Resetting to a given `initvalue` always returns the RNG to the same initial state.
Phrase `9!:0 ''` will return the most recently used `initvalue`.

For RNGs 3 and 4, `initvalue` must not be `0`.

### Use These Combinations

Combinations using ` ? y` that have exceptionally good performance include:

**What it does****Type;**

**Precisions;**

Ranks**Syntax****Variants;**

**Restrictions****Benefits;**

**Bug Warnings**Arrays of random numbers `x ?@$ y`

`x([: ? $)y``?.`in place of`?`

`@:`in place of`@`

`#`in place of`$`does not create `x $ y`

`x ? y`Deal

Rank 0 0 *-- operates on individual atoms of x and y, producing a result that may require fill --*
WHY IS THIS IMPORTANT?

Selects `x` items at random (without repetition) from the list ` i.y`

3?10 4 8 9 3?10 2 4 7

It follows that ` y?y ` permutes the items of (i.e. is a *permutation* of) ` i.y`

10?10 7 2 6 0 5 1 3 8 4 9

### Common uses

1. A random permutation (*scrambling*) of the integers `0` to `9`

10?10 7 2 6 0 5 1 3 8 4 9 ?~10 NB. -can also be written this way. 3 8 9 2 0 6 1 7 5 4

2. Deal 3 cards from a pack of 52 cards

] s=: u: 16b2660 + i.4 NB. suits ♠♡♢♣ d=: 'A23456789TJQK' NB. denominations pack=: , d <@,"0/ s > (3?52) { pack NB. deal 3 cards from 52 5♢ 9♢ 7♣

### Related Primitives

Deal (Fixed seed) (`x ?. y`)

### More Information

1. `x` may not be greater than `y` (after each argument has been rounded to an integer using tolerant comparison).