0

I'm trying to learn swift by myself so sorry if this turns out to be very simple.

I want to make a function that, given two numbers (example: 5, 60) gives back random numbers between those two numbers (example: 33,56,12, but not 2 or 90).

I tried using this line of code on the Playground and it worked

var a : UInt32
var b : UInt32
a = 5
b = 60
let randomNumber = arc4random_uniform(b-a) + a
//gives back a random number between 5 and 60

However, when I create - using the same method - the function randomNumberInBetween in my viewController.swift, it gives me an error.

var a = UInt32()
var b = UInt32()
var randomNumberInBetween = Int()

func randomInBetween() {
    randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
    // here I get this error:   Thread 1 : EXC_BAD_INSTRUCTION (code=EXC_l386_INVOP,subcode=0x0)
}



@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!
}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!
}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    randomInBetween()
    unHide()
    Label.text = "\(randomInBetween)"
    NSLog("\(randomInBetween)")
}

(PS: TextA is a Text field in which the user inserts the value a (ex: 5) TextB is a Text field in which the user inserts the value b (ex: 60) )

I've been trying to work it out by myself but I just lost hope, anyone knows what the problem is? Thank you:)

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
tommsyeah
  • 17
  • 2
  • 9
  • 3
    What values have `a` and `b` when your app crashes? For example, it will crash if `b < a`. And of course `UInt32(TextA.text!)!` crashes if the text field does not contain a string which represents a valid number. – Martin R Jun 20 '16 at 13:45
  • 2
    Do not abuse state like this. Make `randomInBetween` take a `Range` parameter, or two `Int` parameters. – Alexander Jun 20 '16 at 13:47
  • @MartinR it crashes even if a – tommsyeah Jun 20 '16 at 14:05
  • @AMomchilov mm could you explain it better? That might be a solution.. Anyway, even if by changing the function I could avoid the error I really am interested in understanding why that function doesn't work as I'd expect it to.. what did I do wrong? – tommsyeah Jun 20 '16 at 14:09
  • are you sure your actions are being fired, if you put a breakpoint on your random inbetween, does the values of a and b get filled in. For all we know, you have `arc4random_uniform(0 - 0)` which will crash – Knight0fDragon Jun 20 '16 at 14:14
  • @tommsyeah: Try to isolate the problem. Add `print(a, b)` _before_ the arc4random_uniform call, and `print(randomNumberInBetween)` _after_ the call. What is the last output before the crash? – Martin R Jun 20 '16 at 14:14
  • 1
    @Knight0fDragon: Actually arc4random_uniform(0 - 0) does not crash, but returns zero, as one can see in https://opensource.apple.com/source/Libc/Libc-1082.20.4/gen/FreeBSD/arc4random.c . – Martin R Jun 20 '16 at 14:17
  • @MartinR, yes you are right, but then based on this when b < a. we should be getting an overflow exception, which doesn't seem like the correct answer due to his comment, so it appears we may be missing some code here – Knight0fDragon Jun 20 '16 at 14:33
  • @Knight0fDragon: That's why I asked for more debugging output ... – Martin R Jun 20 '16 at 14:34
  • @MartinR your suggestion is helping out: I tried printing a and b in the NSLog just before calling the function and what happens is kinda weird. If I set a to 14 and then b to 89 for example, the app crashes, the error appears, but the NSLog prints this "2016-06-20 16:43:38.015 RandomNumberApp[6268:140236] 14, 0 (lldb) " – tommsyeah Jun 20 '16 at 14:46
  • @MartinR If I try to set b ( = 89) first and then a (= 14) the app crashes, the error appears, and the NSLog prints this : "2016-06-20 16:45:33.895 RandomNumberApp[6290:141668] 0, 89 2016-06-20 16:45:33.895 RandomNumberApp[6290:141668] (Function) 2016-06-20 16:45:33.895 RandomNumberApp[6290:141668] (Function)" Also, the Label.text is equal to "(Function)" so instead of the number what appears is (Function) – tommsyeah Jun 20 '16 at 14:46
  • 1
    I assume that you don't print the values correctly. For NSLog, you would have to use the `%ld` format when printing an `Int`. – Martin R Jun 20 '16 at 14:49
  • @MartinR oops sorry I really don't know how to use that format .. (?) – tommsyeah Jun 20 '16 at 14:59
  • `NSLog("a=%ld, b=%ld", a, b)`. Or simply `print("a =", a, "b=", b)`. – Martin R Jun 20 '16 at 15:01
  • @MartinR thank you. I did the NSLog thing and this happened: If I set a (= 23) first and then b(=97), the app then crashes, the error appears, and the NSLog prints this : "2016-06-20 17:11:00.268 RandomNumberApp[6482:155664] a=23, b=0 (lldb)" If I set b first and then a, app crashes, error appears, NSLog prints this : "2016-06-20 17:11:50.104 RandomNumberApp[6502:156378] a=0, b=97 2016-06-20 17:11:50.105 RandomNumberApp[6502:156378] (Function) 2016-06-20 17:11:50.105 RandomNumberApp[6502:156378] (Function)" I really can't figure out what is going on.. ! – tommsyeah Jun 20 '16 at 15:12
  • @MartinR it seems to me that the second value I set, whether it is a or b, always equals 0 (even if I set it to a different number), or at least this is what the NSLog seems to tell – tommsyeah Jun 20 '16 at 15:13
  • 1
    *Debug* your problem! Set breakpoints in the action methods and see what happens. – Martin R Jun 20 '16 at 15:18
  • @MartinR I set breakpoints and discovered that for some (unknown) reason the second value I set equals 0. If for example I set a to 5 first and then b to 9, b =0. If I set b = 9 first and then a = 5, a = 0. Two things make no sense to me. 1) We does the latter value I set equals 0? 2) Even assuming that, for example, a = 0 and b = 30, shouldn't it give me random numbers between 0 and 30? Why an error? – tommsyeah Jun 20 '16 at 15:39

0 Answers0