-2

I made a variable using arc4random this way:

var a = Int(arc4random_uniform(25))

I want a to be between 0 and 24.

Sometimes it works fine, but sometimes it gives a HUGE value with about 20 characters!

What causes this and how can I fix it?

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
Liad
  • 21
  • 1
  • 4
    `Int(arc4random_uniform(25))` *does* return a number between 0 and 24. The problem must be somewhere else. – Martin R Dec 10 '14 at 18:41

2 Answers2

0

Try this code for your random number value:

var a : Int = arc4random_unifrom(25)

Hope this will help you if you are still having problems.

Bigfoot11
  • 911
  • 2
  • 11
  • 25
-1

Swift can be a little unpredictable about how it infers variable types.

Try explicitly type casting both the variable a and the constant (25). I tested this code and it worked fine:

    var a:Int = 0

    for var i:Int = 0; i < 1000; i++
    {
        a = Int(arc4random_uniform(UInt32(25)))
        println("a = \(a)")
    }
jwlaughton
  • 905
  • 1
  • 6
  • 11