17 people like it.

Random Type with a few modifications

The modified Random type is built on top of System.Random type. It has a member Seed which returns a seed and NextFloat has the same overloads as NextInt (Next in System.Random). The members should further support units of measure.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
type Random(seed: int option) =
  let rndSeed() =
    let t = System.DateTime.Now
    (t.Month + t.Minute) * (t.Millisecond - t.Second) + t.Hour
  let seedN = defaultArg seed (rndSeed())
  
  let random = new System.Random(seedN)

  new () = Random(None)
  new (seed: int) = Random(Some seed)
  
  member x.Seed = seedN

  member x.NextInt () = random.Next()
  member x.NextInt (max: int<'a>) = random.Next(int max) * (max / int max)
  member x.NextInt (min: int<'a>, max: int<'a>) = random.Next(int min, int max) * (max / int max)

  member x.NextFloat () = random.NextDouble()
  member x.NextFloat (max: float<'a>) = random.NextDouble() * max
  member x.NextFloat (min: float<'a>, max: float<'a>) = min + random.NextDouble() * (max - min)

// Usage
let rnd = Random()
printfn "Seed = %d" rnd.Seed
rnd.NextInt() |> printfn "NextInt = %d"
rnd.NextInt(10) |> printfn "NextInt = %d"
rnd.NextInt(-10, 10) |> printfn "NextInt = %d"
rnd.NextFloat() |> printfn "NextFloat = %f"
rnd.NextFloat(10.) |> printfn "NextFloat = %f"
rnd.NextFloat(-10., 10.) |> printfn "NextFloat = %f"
Multiple items
type Random =
  new : unit -> Random
  new : seed:int option -> Random
  new : seed:int -> Random
  member NextFloat : unit -> float
  member NextFloat : max:float<'a> -> float<'a>
  member NextFloat : min:float<'a> * max:float<'a> -> float<'a>
  member NextInt : unit -> int
  member NextInt : max:int<'a> -> int<'a>
  member NextInt : min:int<'a> * max:int<'a> -> int<'a>
  member Seed : int

Full name: Script.Random

--------------------
new : unit -> Random
new : seed:int -> Random
new : seed:int option -> Random
val seed : int option
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
val rndSeed : (unit -> int)
val t : System.DateTime
namespace System
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
property System.DateTime.Now: System.DateTime
property System.DateTime.Month: int
property System.DateTime.Minute: int
property System.DateTime.Millisecond: int
property System.DateTime.Second: int
property System.DateTime.Hour: int
val seedN : int
val defaultArg : arg:'T option -> defaultValue:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.defaultArg
val random : System.Random
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
union case Option.None: Option<'T>
val seed : int
union case Option.Some: Value: 'T -> Option<'T>
val x : Random
member Random.Seed : int

Full name: Script.Random.Seed
member Random.NextInt : unit -> int

Full name: Script.Random.NextInt
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
member Random.NextInt : max:int<'a> -> int<'a>

Full name: Script.Random.NextInt
val max : int<'a>
member Random.NextInt : min:int<'a> * max:int<'a> -> int<'a>

Full name: Script.Random.NextInt
val min : int<'a>
member Random.NextFloat : unit -> float

Full name: Script.Random.NextFloat
System.Random.NextDouble() : float
member Random.NextFloat : max:float<'a> -> float<'a>

Full name: Script.Random.NextFloat
val max : float<'a>
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
member Random.NextFloat : min:float<'a> * max:float<'a> -> float<'a>

Full name: Script.Random.NextFloat
val min : float<'a>
val rnd : Random

Full name: Script.rnd
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Random.Seed: int
member Random.NextInt : unit -> int
member Random.NextInt : max:int<'a> -> int<'a>
member Random.NextInt : min:int<'a> * max:int<'a> -> int<'a>
member Random.NextFloat : unit -> float
member Random.NextFloat : max:float<'a> -> float<'a>
member Random.NextFloat : min:float<'a> * max:float<'a> -> float<'a>
Raw view Test code New version

More information

Link:http://fssnip.net/W
Posted:13 years ago
Author:Oldrich Svec
Tags: random