Range
Range
Range[ min, max, step ] creates a list of evenly spaced numbers.
Usages
Range[ n ]
Range[ n ] returns the list {1, 2, ..., n}.
| Example | in | out |
|---|---|---|
The numbers 1 to 5. |
Range[ 5 ] |
{1, 2, 3, 4, 5} |
| A script variable. | n := 4 |
r = {1, 2, 3, 4} |
Range[ min, max ]
Range[ min, max ] returns the numbers from min to max, in steps of 1.
| Example | in | out |
|---|---|---|
The numbers 2 to 5. |
Range[ 2, 5 ] |
{2, 3, 4, 5} |
A decimal start value; max is not reached. |
Range[ 0.5, 2 ] |
{0.5, 1.5} |
Range[ min, max, step ]
Range[ min, max, step ] starts at min and adds step as long as the result does not pass max. The step can be negative, to count down, and can be a decimal number.
| Example | in | out |
|---|---|---|
Multiples of 5. |
Range[ 0, 20, 5 ] |
{0, 5, 10, 15, 20} |
| Counting down. | Range[ 10, 1, -3 ] |
{10, 7, 4, 1} |
| Decimal steps. | Range[ 0, 1, 0.25 ] |
{0, 0.25, 0.5, 0.75, 1} |
max is only included if it is reached exactly. |
Range[ 1, 2, 0.3 ] |
{1, 1.3, 1.6, 1.9} |
Details
- At most 100 elements:
Range[ 1, 101 ]gives the error Range: prevented generating 101 elements. Maximum is 100. - Counting down requires a negative step:
Range[ 5, 1 ]gives an error,Range[ 5, 1, -1 ]gives{5, 4, 3, 2, 1}.
| Invalid input | Result |
|---|---|
Range[ 0 ], Range[ -3 ], Range[ 5, 1 ], Range[ 1, 5, -1 ] |
Error: the sign of the third argument is inconsistent with the first two arguments |
Range[ 1, 5, 0 ] |
Error: the third argument should not be 0 |
Range[ x ] |
Left unevaluated: the arguments must be numbers. |
See also: ConstantArray, For, Map, RandomInt