How do I generate a new random color every time the view loads?

I have a function that generates the random color. Every time I call the function, it generates the same color. I put the function in viewDidLoad() but it still generates the same color.

Answered by OOPer in 356911022

`viewDidLoad()` would not be called when your view contoller is shown. As the view controller uses the already loaded view.


Please try moving the random color setting code from `viewDidLoad()` to `viewWillApeear(_:)`.


In some cases, `viewWillApeear(_:)` would not be called neither, so please tell me more details if you cannot make it with `viewWillApeear(_:)`.

Please show your current code.

func generateRandomColor() -> UIColor {
    let redValue = CGFloat(drand48())
    let greenValue = CGFloat(drand48())
    let blueValue = CGFloat(drand48())
        
    let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
        
    return randomColor
    }



override func viewDidLoad() {
        super.viewDidLoad()
       
        if mainPhoto.backgroundColor == nil {
            mainPhoto.backgroundColor = generateRandomColor()
        }
        else {
           mainPhoto.backgroundColor = generateRandomColor()
        }
        
    }


I am trying to change the background color of an image every time the view loads. Even if I close the app and re-open it, it still generates the same color. Why is that?

Some random library uses exactly the same initial settings at each time the process is initialized.

And `drand48()` is definitely one of them.


So,

if I close the app and re-open it, it still generates the same color. Why is that?

That is the expected behavior with your code.


Why are you using such outdated old C library function?

Try something like this:

func generateRandomColor() -> UIColor {
    let redValue = CGFloat.random(in: 0...1)
    let greenValue = CGFloat.random(in: 0...1)
    let blueValue = CGFloat.random(in: 0...1)
    
    let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
    
    return randomColor
}

The method `randome(in:)` uses better algorithm and random seed for initial value, which is something you expect.


---

By the way what do you intend with this?

        if mainPhoto.backgroundColor == nil {
            mainPhoto.backgroundColor = generateRandomColor()
        }
        else {
            mainPhoto.backgroundColor = generateRandomColor()
        }

The if-statement has no meaning here... Some sort of not-implemented-yet feature?

When I googled how to generate a random color the drand library function came up so I used it. Yes, the random(in: 0...1) function works better but there is one other thing that I need help with. When I tap the back button in the navigation controller, a new custom color isn't generated. Does the view not load when I tap the back button? How can I get it to generate a new random color when I tap the back button?

Accepted Answer

`viewDidLoad()` would not be called when your view contoller is shown. As the view controller uses the already loaded view.


Please try moving the random color setting code from `viewDidLoad()` to `viewWillApeear(_:)`.


In some cases, `viewWillApeear(_:)` would not be called neither, so please tell me more details if you cannot make it with `viewWillApeear(_:)`.

Yes, viewWillAppear() worked. Thank you.

Put your function in viewWillAppear(). It will be called every time the view appear while viewDidLoad() is called once upon the app starting
@Sergehk
you just repeat what OOPer explained 1 year ago…
You'd better read the answers, not only the initial question.

Have a good day.
How do I generate a new random color every time the view loads?
 
 
Q