Difference between revisions of "Code Snippet: Random Strings"

From Coder Merlin
(Created page with "== Select a Random String from an Array == <syntaxhighlight lang="swift"> // Select a random string from an array and print the result let verbs = ["walk", "jump", "run", "swi...")
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Select a Random String from an Array ==
= Select a Random String from an Array =
 
== Swift ==
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
// Select a random string from an array and print the result
// Select a random string from an array and print the result
Line 15: Line 17:
</syntaxhighlight>
</syntaxhighlight>


== Shuffle an Array ==
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
let verbs = ["walk", "jump", "run", "swim", "dance"]
// Select multiple random strings from two arrays and print the results
let shuffledVerbs = verbs.shuffled()
let geographicFeatures = ["river", "lake", "ocean", "mountain", "desert"]
for verb in shuffledVerbs {
let verbAndPrepositions = ["walk to", "jump over", "run to", "dance around"]
     print(verb)
 
for _ in 0 ..< Int.random(in: 3...5) {
    let verb = verbAndPrepositions.randomElement()!
    let geographicFeature = geographicFeatures.randomElement()!
     print("\(verb) the \(geographicFeature)")
}
}
</syntaxhighlight>
== Python ==
<syntaxhighlight lang="python">
import random
verbs = ["walk", "jump", "run", "swim", "dance"]
verb = random.choice(verbs)
print(verb)
</syntaxhighlight>
<syntaxhighlight lang="python">
import random
verbs = ["walk", "jump", "run", "swim", "dance"]
verb =  verbs[random.randint(0, len(verbs)-1)]
print(verb)
</syntaxhighlight>
== Java ==
<syntaxhighlight lang="java">
// Select a random string from an array and print the result
let verbs = {"walk", "jump", "run", "swim", "dance"}
// We'll use the Random class to generate a random number between 0 and verbs.length-1 (don't forget to import the class)
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 10:11, 15 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Select a Random String from an Array[edit]

Swift[edit]

// Select a random string from an array and print the result
let verbs = ["walk", "jump", "run", "swim", "dance"]                                                                                                                                                                                                                                                                                                                
let verb = verbs[Int.random(in:0..<verbs.count)]                                                                                                                                                                                                                                                                                                                    
print(verb)
// Select a random string from an array and print the result (with syntactic sugar)
let verbs = ["walk", "jump", "run", "swim", "dance"]
if let verb = verbs.randomElement() {// Note: randomElement() returns an optional string
    print(verb)
}
// Select multiple random strings from two arrays and print the results 
let geographicFeatures = ["river", "lake", "ocean", "mountain", "desert"]
let verbAndPrepositions = ["walk to", "jump over", "run to", "dance around"]

for _ in 0 ..< Int.random(in: 3...5) {
    let verb = verbAndPrepositions.randomElement()!
    let geographicFeature = geographicFeatures.randomElement()!
    print("\(verb) the \(geographicFeature)")
}

Python[edit]

import random

verbs = ["walk", "jump", "run", "swim", "dance"]
verb = random.choice(verbs)
print(verb)
import random

verbs = ["walk", "jump", "run", "swim", "dance"]
verb =  verbs[random.randint(0, len(verbs)-1)]
print(verb)

Java[edit]

// Select a random string from an array and print the result
let verbs = {"walk", "jump", "run", "swim", "dance"}
// We'll use the Random class to generate a random number between 0 and verbs.length-1 (don't forget to import the class)