Swift Random Double Generator

import Foundation

import CoreGraphics

public extension Double {

      public static func random() -> Double {

        return Double(arc4random()) / 0xFFFFFFFF

    }

    public static func random(min min: Double, max: Double) -> Double {

        return Double.random() * (max – min) + min

    }

}

var sayac: Int = 0

//formul 100 >= x^2 + y^2

var randomNumDoubleX : Double = 0.00

var randomNumDoubleY : Double = 0.00

var Xkr : Double = 0.00

var Ykr : Double = 0.00

for i in 0..<1000000{

    // x ve y için min : 0.00 max: 10.00

    randomNumDoubleX  = Double.random(min: 0.00, max: 10.00)

    Xkr = Double( String(format: “%.2f”, randomNumDoubleX))!

    randomNumDoubleY = Double.random(min: 0.00, max: 10.00)

    Ykr = Double( String(format: “%.2f”, randomNumDoubleY))!

    

    if 100 >= (Xkr*Xkr) + (Ykr*Ykr) {

        sayac+=1

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: