1

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.

Community
  • 1
  • 1
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • 1
    More 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 Answers1

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
  • 2
    I 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
  • 2
    We'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
    @holex `000,000,000` is 9 digits, but also less than `100,000,000` – Alexander Aug 17 '16 at 14:47
  • @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
  • @AlexanderMomchliov I want not to allow leading or trailling zeros. – bibscy Aug 17 '16 at 14:58
  • 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
  • @AlexanderMomchliov that is correct. – bibscy 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
    @bogdanbarbulescu I've edited my answer – Alexander Aug 17 '16 at 15:14
  • @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
    @holex reddit.com/r/iamverysmart is just for you. – Alexander Aug 17 '16 at 15:25
  • @AlexanderMomchliov, it was the poorest comeback ever. – holex Aug 17 '16 at 15:30
  • 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