<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Swift 5 Archives - AppMakers.Dev</title>
	<atom:link href="https://appmakers.dev/tag/swift-5/feed/" rel="self" type="application/rss+xml" />
	<link>https://appmakers.dev/tag/swift-5/</link>
	<description>SwiftUI Tutorials, iOS App Development, SwiftUI, Swift</description>
	<lastBuildDate>Tue, 29 Oct 2024 22:01:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://appmakers.dev/wp-content/uploads/2024/10/cropped-AppMakersDev-32x32.jpg</url>
	<title>Swift 5 Archives - AppMakers.Dev</title>
	<link>https://appmakers.dev/tag/swift-5/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Generate Random numbers in Swift</title>
		<link>https://appmakers.dev/generate-random-numbers-in-swift/</link>
		
		<dc:creator><![CDATA[AppMakers]]></dc:creator>
		<pubDate>Sat, 03 Aug 2019 16:42:12 +0000</pubDate>
				<category><![CDATA[Export Free]]></category>
		<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[Swift Tutorials]]></category>
		<category><![CDATA[iOS Tutorial]]></category>
		<category><![CDATA[Random Numbers]]></category>
		<category><![CDATA[Swift 5]]></category>
		<category><![CDATA[Swift Tutorial]]></category>
		<guid isPermaLink="false">https://appmakers.dev/?p=936</guid>

					<description><![CDATA[<p>In this tutorial, you will learn How to Generate Random numbers in Swift. You will Need Mac, the latest Xcode to complete this tutorial. Open Xcode and create a new playground titled &#8220;How to generate Radom Numbers&#8221; Create two variables to represent the limits. var minNumber = 0 var maxNumber = 300 Now let&#8217;s use the random()&#8230;</p>
<p>The post <a href="https://appmakers.dev/generate-random-numbers-in-swift/">Generate Random numbers in Swift</a> appeared first on <a href="https://appmakers.dev">AppMakers.Dev</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this tutorial, you will learn <strong>How to Generate Random numbers in Swift.</strong></p>
<p>You will Need Mac, the latest Xcode to complete this tutorial.</p>
<p>Open Xcode and create a new playground titled &#8220;How to generate Radom Numbers&#8221;</p>
<p>Create two variables to represent the limits.</p>
<pre><code class="language-swift" data-line="">var minNumber = 0
var maxNumber = 300</code></pre>
<p>Now let&#8217;s use the <strong>random()</strong> function to generate a random number between 0 and 300.</p>
<pre><code class="language-swift" data-line="">var randomNumberValue = Int.random(in: minNumber...maxNumber)</code></pre>
<p>Run and see the results, now you know how to generate Random number in Swift.</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-937 size-large" src="https://appmakers.dev/wp-content/uploads/2019/08/Screenshot-2019-08-03-at-18.47.03.png" alt="" width="980" height="305" srcset="https://appmakers.dev/wp-content/uploads/2019/08/Screenshot-2019-08-03-at-18.47.03.png 1278w, https://appmakers.dev/wp-content/uploads/2019/08/Screenshot-2019-08-03-at-18.47.03-300x93.png 300w" sizes="auto, (max-width: 980px) 100vw, 980px" /></p>
<p>We can improve our code a little bit to show values between 1 and 1000 printed in a console:</p>
<pre><code class="language-swift" data-line="">for _ in 1...1000 {
    print(Int.random(in: 1...1000))
}</code></pre>
<p>Run and see the results:</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-938 size-medium" src="https://appmakers.dev/wp-content/uploads/2019/08/Random2-300x213.png" alt="" width="300" height="213" srcset="https://appmakers.dev/wp-content/uploads/2019/08/Random2-300x213.png 300w, https://appmakers.dev/wp-content/uploads/2019/08/Random2.png 1018w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>If you have the array of Ints and want to get a random value out of it, you can do it with the <strong>shuffle()</strong> or <strong>shuffled() .</strong> Type these lines and Run.</p>
<pre><code class="language-swift" data-line="">var array = [18,323,34,22,9078,65]

print(&quot;array after shuffled(): \(array.shuffled())&quot;)

array.shuffle() // shuffle in place

print(&quot;array after shuffle(): \(array)&quot;)</code></pre>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-939 size-full" src="https://appmakers.dev/wp-content/uploads/2019/08/Random3_result.png" alt="" width="790" height="389" srcset="https://appmakers.dev/wp-content/uploads/2019/08/Random3_result.png 790w, https://appmakers.dev/wp-content/uploads/2019/08/Random3_result-300x148.png 300w" sizes="auto, (max-width: 790px) 100vw, 790px" /></p>
<p>BTW, we can use random() not only for Ints.</p>
<p>We can use it for Bool, Floats and Doubles too.</p>
<p>Just test bool randomness with these lines:</p>
<pre><code class="language-swift" data-line="">let boolRandomized = Bool.random()
print(boolRandomized)

for _ in 1...20 {
    print(Bool.random())
}</code></pre>
<p>Check floats and doubles too:</p>
<pre><code class="language-swift" data-line="">for _ in 1...20 {
  
    let floatRandom = Float.random(in: 0..&lt;79)
    print(floatRandom)

}



for _ in 1...20 {
   
    let doubleRandom = Double.random(in: 0.3...138.90)
    print(doubleRandom)

}</code></pre>
<p>In this tutorial you&#8217;ve learned how to Generate Random numbers in Swift.</p>
<p>The post <a href="https://appmakers.dev/generate-random-numbers-in-swift/">Generate Random numbers in Swift</a> appeared first on <a href="https://appmakers.dev">AppMakers.Dev</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
