---
# System prepended metadata

title: Swift - random
tags: [Swift]

---

---
title: 'Swift - random'
---


## Random number 

### Swift 4.2 and higher

```swift=
//from 0 to 100, not including 100
var number = Int.random(in: 0..<100)
print(number)

//from 0 to 100, including 100
number = Int.random(in: 0...100)
print(number)

let fraction = Float.random(in: 0..<1)
print(fraction)

let boolean = Bool.random()
print(boolean)
```
**output** 
> 48
36
0.35037667
true

###  lower than 4.2


```swift=
import Foundation

//from 0 to 999
var number1 = Int(arc4random_nuiform(1000))
print(number1)

var number2 = Int(arc4random() % 1000)
print(number2)
```
**output** 
>339
>76

###### tags: `Swift`
