# How do you pick a random string from an array?

**URL:** <https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368>\
**Category:** Swift\
**Created:** [November 13, 2018, 6:11pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368 "2018-11-13T18:11:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjl037](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tjl037/32/79544_2.png) [@tjl037](https://forums.kodeco.com/u/tjl037)\
**Post date:** [November 13, 2018, 6:11pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/1 "2018-11-13T18:11:41Z")

</div>

Hello there, how do you pick a random string in Swift from an array? for example;

let names = [“Timmy”, “Von”, “Bimmy”]

Thank you.

---

<div class="post-metadata">

**Author:** ![anon64978853](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon64978853](https://forums.kodeco.com/u/anon64978853)\
**Post date:** [November 13, 2018, 6:52pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/2 "2018-11-13T18:52:01Z")

</div>

Guess this could be an option:

```
// For example:
let names = ["Timmy", "Von", "Bimmy", "Ray", "Wenderlich"]
let randomNumber = Int.random(in: 0..<names.count)

print("Random string:", names[randomNumber])

```

Run this a few times.

---

<div class="post-metadata">

**Author:** ![tjl037](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tjl037/32/79544_2.png) [@tjl037](https://forums.kodeco.com/u/tjl037)\
**Post date:** [November 13, 2018, 7:17pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/3 "2018-11-13T19:17:18Z")

</div>

It worked, thank you so much! If you wouldn’t mind answering another question, how would you print the result in the message of an alert?

---

<div class="post-metadata">

**Author:** ![anon64978853](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon64978853](https://forums.kodeco.com/u/anon64978853)\
**Post date:** [November 13, 2018, 8:03pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/4 "2018-11-13T20:03:11Z")

</div>

Not sure what you mean by that, like in an alert popup? Might want to provide some context and I’ll see what I can do 🙂

---

<div class="post-metadata">

**Author:** ![tjl037](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tjl037/32/79544_2.png) [@tjl037](https://forums.kodeco.com/u/tjl037)\
**Post date:** [November 13, 2018, 8:16pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/5 "2018-11-13T20:16:30Z")

</div>

Yeah, I meant in an alert popup. Sorry about that!

For example:

You know how you create an alert with an UIAlertController and you can specify the title, and message, and preferredStyle. I would like the random string to show in the message of the alert.

---

<div class="post-metadata">

**Author:** ![anon64978853](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon64978853](https://forums.kodeco.com/u/anon64978853)\
**Post date:** [November 13, 2018, 9:13pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/6 "2018-11-13T21:13:47Z")

</div>

No worries. Yeah you’ll have to do so like the following, for example:

```
let names = ["Timmy", "Von", "Bimmy", "Ray", "Wenderlich"]

@IBAction func showAlert() { 
    // ^ Hook this up to a button via Interface Builder

    let randomNumber = Int.random(in: 0..<names.count)
    
    let alertController = UIAlertController(title: nil, message: names[randomNumber], preferredStyle: .alert)
    let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertController.addAction(alertAction)
    
    self.present(alertController, animated: true, completion: nil)
}

```

Example project : [Click me to download](https://www.dropbox.com/s/p8wf7uqgi85cdnx/test.zip?dl=0)  
Hope this is what you’re looking for, if not, contact me via Twitter [@PieterVelghe](https://twitter.com/PieterVelghe)

---

<div class="post-metadata">

**Author:** ![tjl037](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tjl037/32/79544_2.png) [@tjl037](https://forums.kodeco.com/u/tjl037)\
**Post date:** [November 13, 2018, 9:41pm UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/7 "2018-11-13T21:41:20Z")

</div>

That’s awesome! thank you so much 🙂

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [November 14, 2018, 7:33am UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/8 "2018-11-14T07:33:52Z")

</div>

Hi @tjl037,  
a couple of things for you

1. If you want to get a random string from an array then the newer release of Swift has made it simple, you can simply use the randomElement() function on the array - as in

```auto
  let names = ["timmy", "tommy", "tammy", "tabby", "tubby", "tooby"]
  print(names.randomElement()!)

```

1. You want to display alerts you can create an extension that can allow you to display the alert or a function that can have a signature like

```auto
  func showAlert(title: String = "", message: String) { ... }

```

this way you can pass it a title and or a message

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 29, 2019, 10:11am UTC](https://forums.kodeco.com/t/how-do-you-pick-a-random-string-from-an-array/55368/9 "2019-04-29T10:11:41Z")

</div>

Closed on April 29, 2019, 10:11am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
