How can I generate a 9 digits number in Swift. I have tried random() function, but the size of the generated number is different every time the function is called. I have also tried this code enter link description here , but it is deprecated.
Asked
Active
Viewed 2,937 times
1
-
1More solutions here: [How does one make random number between range for arc4random_uniform()?](http://stackoverflow.com/questions/24132399/how-does-one-make-random-number-between-range-for-arc4random-uniform) and here: [How does one generate a random number in Apple's Swift language?](http://stackoverflow.com/questions/24007129/how-does-one-generate-a-random-number-in-apples-swift-language) – What you probably want is a random number in the range 100000000 .. 999999999. Solutions are given in the linked-to Q&As. – Martin R Aug 17 '16 at 14:32
-
how about... `let random: UInt32 = arc4random_uniform(899_999_999) + 100_000_000`? – holex Aug 17 '16 at 14:32
-
@holex: that will never generate `999 999 999` :) (`arc4random_uniform(n) -> 0..
– dfrib Aug 17 '16 at 14:43
1 Answers
5
import Foundation
func random9DigitString() -> String {
let min: UInt32 = 100_000_000
let max: UInt32 = 999_999_999
let i = min + arc4random_uniform(max - min + 1)
return String(i)
}

Alexander
- 59,041
- 12
- 98
- 151
-
2I think OP is looking for `... = 100000000 + arc4random_uniform(1000000000-100000000)`. – dfrib Aug 17 '16 at 14:30
-
and since when 9 digit numbers the e.g. 8, 7, 6, 5, 4, 3, 2, 1 digit numbers? – holex Aug 17 '16 at 14:33
-
2We'll need more context from OP. I suspect he'll want numbers in the range 0...100000000 too. Thus these will need to be zero-padded Strings – Alexander Aug 17 '16 at 14:36
-
@AlexanderMomchliov, I hardly imagine if the OP wants _9 digits numbers_ they would be happy any number under 100.000.000 which definitely have _less_ than 9 digits, huh? – holex Aug 17 '16 at 14:40
-
@Alexander Momchliov I need to generate numbers with a length of exactly 9 digits. – bibscy Aug 17 '16 at 14:41
-
1@bogdanbarbulescu Would you want numbers like `000,000,000`, `000,000,001`, ..., `099,999,999`? – Alexander Aug 17 '16 at 14:44
-
1
-
@bogdanbarbulescu and see my comment above if you're instead looking to generate an integer in the range `[100 000 000, 999 999 999]`. – dfrib Aug 17 '16 at 14:48
-
@AlexanderMomchliov I want random 9 digit numbers like 394810472, 048759274... and so on, random ones. – bibscy Aug 17 '16 at 14:48
-
@bogdanbarbulescu `048759274` is (if stored as a number) `48759274`, an 8-digit number... – dfrib Aug 17 '16 at 14:50
-
@dfri I will store them as String type, but it would be more convenient if the 1st digit would not be 0 – bibscy Aug 17 '16 at 14:52
-
@bogdanbarbulescu _"more convenient"_ is vague: decide wether you want to allow padding zeros or not, and AlexanderM can probably sort out this answer to your specifications. – dfrib Aug 17 '16 at 14:55
-
-
1@bogdanbarbulescu So then to clarify the specifications: you want to randomly generate numbers in the range: `100,000,000` to `999,999,999`, inclusive, stored as strings? – Alexander Aug 17 '16 at 15:01
-
-
@bogdanbarbulescu re your other comment. You don't want trailing zeros? – Matt Le Fleur Aug 17 '16 at 15:12
-
1
-
@AlexanderMomchliov, yeah, let us not go with how many digits have the `000000001` string, because it is has zero _digits_, that has _characters_ only. – holex Aug 17 '16 at 15:23
-
1
-
-
1@holex I'm having a legitimate discussion with OP on his requirements. You're dismissing my questions on some irrelevant technicality, which also happens to wrong. Either `String` nor `Int` technically have digits. `Int`'s manifestation as a decimal number composed of digits is purely a facade for our convenience. There's nothing but bits, if you want to be technical about it. – Alexander Aug 17 '16 at 15:37
-
@AlexanderMomchliov, I'm really sorry, but I have a few other things to do as well during, rather than educate the difference between the word _digit_ and _character_ to you – and the OP has already confirmed that my interpretation was the correct, not yours. – holex Aug 17 '16 at 15:40
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121184/discussion-between-alexander-momchliov-and-holex). – Alexander Aug 17 '16 at 15:43