Steve
Steve

Reputation: 1153

How to make random float in swift with math operations

Get an error when trying to use multiplication

var red1 : Float = ((random()%9)*30)/255;

Error says Int is not convertible to Float

I want to use this to make a float I can use to make a UIColor

Upvotes: 2

Views: 469

Answers (1)

Schemetrical
Schemetrical

Reputation: 5536

var red1 = Float((random()%9)*30)/255

You don't need the semicolon and red1 will be type inferred to a Float.

In swift 4.2: var red1 = Float.random(in: startNumber...endNumber)

Upvotes: 2

Related Questions