Linked Questions
40 questions linked to/from How to generate a random number in Swift?
5 votes
2 answers
12k views
How do you generate a random number in swift? [duplicate]
tl:dr; How do I generate a random number, because the method in the book picks the same numbers every time. This seems to be the way in Swift to generate a random number, based on the book released ...
5 votes
1 answer
4k views
How to generate a random number in a range (10...20) using Swift [duplicate]
I'm able to pick a random number for my items in my game but is it possible to pick a random number between 2 numbers? so instead of let number = (arc4random_uniform(100)) I would like something ...
0 votes
2 answers
879 views
Generate a random number in swift and then add another number to it? [duplicate]
How do I generate a random number in Swift language that can be used in math, such as addition. Basically I just want to be able to generate a random number and then add it to a count that I have. For ...
0 votes
1 answer
827 views
Generating random number 1 - 100 in Swift 2 [duplicate]
I was wondering if this is the best way to generate a random number in Swift. This is what I have thought of thus far: var randomNumber:Int = random() * 100 + 1 //What value does this return? I was ...
0 votes
1 answer
539 views
How to find a random number between UInt64? (Swift) [duplicate]
I'm having trouble trying to find a way to return a random number between 1 and 9,000,000,000,000,000 I have made a randomNumberBetween function using UInt32 but I just havn't been able to do the ...
0 votes
1 answer
429 views
How can I randomize some Firebase Firestore documents? [duplicate]
How sort the documents in a collection randomly. This is the function I use to get the documents, how can I sort them randomly? func fetchExplore() { let query = COLLECTION_POSTS.limit(to: 6) ...
0 votes
1 answer
297 views
Swift How To Take Int from two textfield and generate a random number [duplicate]
I have to text fields on a differnet scene.One text field has a smaller number and another one has a bigger number.I need to take those numbers and generate a random number using the smallest number ...
-5 votes
1 answer
79 views
Cant seem to get past this line of code [duplicate]
What am I doing wrong? It won't let me use random. PickRandom() func PickRandom() { let RandomNumber = random() % QuizObject.count // error here Btn1.setTitle(QuizObject[RandomNumber]....
344 votes
10 answers
79k views
Why do people say there is modulo bias when using a random number generator?
Why exactly is there "modulo bias" when using a random number generator, like rand() in C++?
44 votes
3 answers
19k views
Creating random Bool in Swift
I'm needing to create a random bool value in my game, in Swift. It's basically, if Yes (or 1), spawn one object, if No (or 0), spawn the other. So far, looking at this question and a similar one on ...
55 votes
1 answer
51k views
What's the difference between arc4random and arc4random_uniform? [duplicate]
I've seen old posts about the differences between random and arc4random in Objective-C, and I've seen answers to this online but I didn't really understand, so I was hoping someone here could explain ...
28 votes
7 answers
8k views
Crash when casting the result of arc4random() to Int
I've written a simple Bag class. A Bag is filled with a fixed ratio of Temperature enums. It allows you to grab one at random and automatically refills itself when empty. It looks like this: class ...
14 votes
5 answers
20k views
How to generate a random number in Swift without repeating the previous random number?
I'm pretty new to Swift and programming logic in general so bear with me How can you generate a random number between 0 and 9 in Swift without repeating the last generated number? As in the same ...
14 votes
4 answers
5k views
Swift arc4random_uniform(max) in Linux
I'm working with Swift in Ubuntu, and I am getting an error that arc4random is an unresolved identifier. More information on this known bug here. Basically, the function only exists in BSD distros. I'...
12 votes
1 answer
2k views
Arc4random modulo biased
According to this documentation, arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two. How bad ...