0

I am making a 'Guess The Number' Game for a project at school, I want my application to generate one random number between 1-10 and then let me use it under my button which allows the user to guess the number by entering their guess into a text field. I have managed to do some of this, my random number generator is currently under my 'Guess' Button which means every time they press the button the number will change. My solution is to generate a number under super.viewDidLoad and then use it under the button to do the rest, but I'm not sure how to do this.

Jason
  • 635
  • 1
  • 9
  • 21
Mike BRA
  • 19
  • 4
  • 4
    Post some code please – Yury Jul 08 '16 at 05:56
  • 1
    I guess you mean just 'viewDidLoad', except you've got a complex ViewController-structure with inheritance – TMob Jul 08 '16 at 06:03
  • http://stackoverflow.com/questions/5601953/generate-random-number-in-range-in-ios?rq=1 Here is the Obj C version I think you can convert it to swift – Bhumit Mehta Jul 08 '16 at 06:07
  • He doesn't want to know how to generate numbers randomly, he wants to know when and how to initialize them. – TMob Jul 08 '16 at 06:13
  • Yes I know how to generate a random number, but I dont want it to generate under my button, I just want it to generate the number asoon as the program runs. – Mike BRA Jul 08 '16 at 06:19
  • Just a comment here: when you create it only when loading your view, once the user guessed the number correctly he will have to restart your game. It'd be cleaner to move the logic into a seperate method which you can call in your viewDidLoad-method and also when the user guesses correctly. – TMob Jul 08 '16 at 06:52

2 Answers2

0

Use the standard library functions for high quality random numbers: arc4random() or arc4random_uniform(), just as in Objective-C.

They are in the Darwin module, so you will need to import Darwin if you haven't imported AppKit, UIKit, or Foundation.

Ashish Thakkar
  • 944
  • 8
  • 27
0

If I've understood you correctly you want the number to be generated only once instead of on every press.

So you might want something like this:

var randomNumber: Int

override func viewDidLoad() {
    super.viewDidLoad()

    //Initialization
    generateRandomNumber()
}

func generateRandomNumber() {
    randomNumber = Int(arc4random_uniform(9)) + 1
}

randomNumber is an instance-variable, so you can access it in all of your methods, for example to check if the user guessed correctly.

TMob
  • 1,278
  • 1
  • 13
  • 33
  • Yes you do understand me correctly thanks ill try it! – Mike BRA Jul 08 '16 at 06:11
  • just a question. after "randomNumber =" do i put arc4random? – Mike BRA Jul 08 '16 at 06:16
  • Yes or whatever you've already used to generate random numbers. From your question I read that the random-algorithm already works. Just move it from the "button-pressed" method to here. – TMob Jul 08 '16 at 06:17
  • Okay, I am using ' Double(arc4random() % 11)' but when I typed it in it says "Cannot assign value of type 'Double' to type 'Int' " – Mike BRA Jul 08 '16 at 06:21
  • Ah well Double is a different data-type than Int. Double are floating-point numbers, which dont make a lot of sense for a guessing-game as nobody wants to guess 4.5612. Int are only the natural numbers so 1, 2, 3, 4... and so on – TMob Jul 08 '16 at 06:24
  • Okay I get it now, so what should I do? – Mike BRA Jul 08 '16 at 06:25
  • `arc4random_uniform(9)+1` let me explain :) This is a bit cleaner than your modulo-method. arc4random_uniform(9) will produce a number from 0 to 9. And by adding 1 it will then fit your range from 1 to 10 – TMob Jul 08 '16 at 06:26
  • This makes perfect sense, but now its telling me "Cannot convert value of UInt32 to expected argument type Int" – Mike BRA Jul 08 '16 at 06:30
  • Check my answer again, I added it there. Haven't worked with Swift for some time :D What this does it creates a new Int-value with the UInt32-value returned from the random-method because UInt32 can't be casted to Int directly – TMob Jul 08 '16 at 06:45
  • You're welcome. I've changed my answer a bit, I moved the logic into a method so you can call it also when the user guessed correctly. – TMob Jul 08 '16 at 06:54
  • Thanks for your efforts but Its okay, I have already made a algorithm that checks if the answer is correct. Thanks anyway. I hope you dont mind that if I have any other minor questions could I just ask you? – Mike BRA Jul 08 '16 at 06:56
  • Going to be difficult as SO doesn't have a PM system. You can try posting it here as a comment – TMob Jul 08 '16 at 06:57