'How to generate a fixed random number in swift

I want to generate a random color for my object, the same object keeps the same color.

The code now generates unpredictable random numbers every time

Color(
   red: Double.random(in: 0...1),
   green: Double.random(in: 0...1),
   blue: Double.random(in: 0...1)
)


Solution 1:[1]

This is a simple solution. You can create a UIColor object and store it the same way you would store anything else.

let fixedRandomColor = UIColor(red: Double.random(in: 0...255)/255, green: Double.random(in: 0...255)/255, blue: Double.random(in: 0...255)/255, alpha: 1)

Use fixedRandomColor wherever you want

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Minasyan