This is the first in a series of articles demonstrating some software engineering concepts in F# programming. The vehicle for my demonstrations is RandomBits, which derives random numbers from bits supplied by the ANU Quantum Random Numbers Server. Good pseudo-random number generators are perfectly adequate for almost any random number generation need you may ever encounter, but the application is nonetheless interesting from a software engineering standpoint. It consumes a web service, does bit-twiddling, implements generic functions, and presents a nice set of testing requirements.
F# Generic Functions
RandomBits provides true (not pseudo-random) signed and unsigned 8, 16, 32, and 64 bit random numbers from the bits served up from the Australian National University Centre for Quantum Computation & Communication Technology. As you can well imagine the basic processing for different bit-sized integers is essentially the same, yet most typed languages would limit code sharing among different data types. Chapter 5 of Expert F# 3.0 describes three techniques for writing generic algorithm functions, allowing you to write functions which support all numeric types.
I chose the inlining technique for this project. Take a look at the first snippet:
216: member inline private this.rndSignRangeSeq (inclLower : 'a) (exlUpper : 'a) length 217: (nextZero : 'b) (nextOne : 'b) (nextSigned : 'a -> 'b) (signed : 'b -> 'a) = 218: 219: let lower = nextSigned inclLower 220: let range = (nextSigned exlUpper) - lower 221: 222: if range < nextOne then 223: invalidArg "range" (sprintf "%i %i range must be greater than 0" inclLower exlUpper) 224: if length < 1 then 225: invalidArg "length" (sprintf "%i must be greater than 0" length) 226: 227: let upper = (nextSigned exlUpper) - (lower + nextOne) 228: 229: seq {for i = 1 to length do 230: yield this.randomWalk lower upper nextZero nextOne |> signed 231: }
rndSignRangeSeq
validates user parameters and generates a sequence of signed random numbers of any type from sbyte to int64. Because there are arithmetic operations involved, generically typing the input parameters is not sufficient to use this function for more than one number type. The input parameters operated upon by +, -, < would default to the first number type passed by another function and attempts to use additional types would fail at compile time, but by inlining this member the input parameters stay generic and the F# compiler assigns an implicit constraint that the generic be a type supporting the math operators involved. Mouse-over one of the range delimter parameters (inclLower
or exlUpper
) and you will see
'a (requires 'a: (byte|inte16|int32|int64|sbyte|uint16|uint32|uint64|nativeint|unativeint))
Part of the logic (in the succeeding generic function randomWalk
, not described in this article) requires casting and computations at the next larger size of number. Note the parameters of type 'b
'b (requires member (-) and comparison and member (+) and member (>>>) and member (<<<))
whose implicit constraints the compiler also derives. When writing the code you just need to keep track of the roles of types 'a
and 'b
.
This allows simple code that can handle all signed number types. Here are the public members for different kinds of random ranges calling rndSignRangeSeq
489: (* random numbers in range sequences *) 490: 491: ///random signed 8-bit integer in range inclusive of lower and exclusive of upper, seq of length 492: member this.RndSByteSeq (inclLower, exlUpper, length) = 493: this.rndSignRangeSeq inclLower exlUpper length 0s 1s (int16) (sbyte) ... 498: 499: ///random signed 16-bit integer in range inclusive of lower and exclusive of upper, seq of length 500: member this.RndInt16Seq (inclLower, exlUpper, length) = 501: this.rndSignRangeSeq inclLower exlUpper length 0 1 (int32) (int16) ... 506: 507: ///random signed 32-bit integer in range inclusive of lower and exclusive of upper, seq of length 508: member this.RndInt32Seq (inclLower, exlUpper, length) = 509: this.rndSignRangeSeq inclLower exlUpper length 0L 1L (int64) (int) ... 513: this.rndUnsignRangeSeq inclLower exlUpper length 0u 1u 514: 515: ///random signed 64-bit integer in range inclusive of lower and exclusive of upper, seq of length 516: member this.RndInt64Seq (inclLower, exlUpper, length) = 517: this.rndSignRangeSeq inclLower exlUpper length 0I 1I (this.bigint) (int64)
Full name: Microsoft.FSharp.Core.Operators.int
Full name: Microsoft.FSharp.Core.Operators.int64
type: RandomBits
implements: IDisposable
Full name: RandomBits.waiting
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
Full name: Microsoft.FSharp.Core.Operators.not
BackgroundWorker.RunWorkerAsync(argument: obj) : unit
class
inherit System.Runtime.ConstrainedExecution.CriticalFinalizerObject
new : System.Threading.ThreadStart -> System.Threading.Thread
new : System.Threading.ThreadStart * int -> System.Threading.Thread
new : System.Threading.ParameterizedThreadStart -> System.Threading.Thread
new : System.Threading.ParameterizedThreadStart * int -> System.Threading.Thread
member Abort : unit -> unit
member Abort : obj -> unit
member ApartmentState : System.Threading.ApartmentState with get, set
member CurrentCulture : System.Globalization.CultureInfo with get, set
member CurrentUICulture : System.Globalization.CultureInfo with get, set
member DisableComObjectEagerCleanup : unit -> unit
member ExecutionContext : System.Threading.ExecutionContext
member GetApartmentState : unit -> System.Threading.ApartmentState
member GetCompressedStack : unit -> System.Threading.CompressedStack
member GetHashCode : unit -> int
member Interrupt : unit -> unit
member IsAlive : bool
member IsBackground : bool with get, set
member IsThreadPoolThread : bool
member Join : unit -> unit
member Join : int -> bool
member Join : System.TimeSpan -> bool
member ManagedThreadId : int
member Name : string with get, set
member Priority : System.Threading.ThreadPriority with get, set
member Resume : unit -> unit
member SetApartmentState : System.Threading.ApartmentState -> unit
member SetCompressedStack : System.Threading.CompressedStack -> unit
member Start : unit -> unit
member Start : obj -> unit
member Suspend : unit -> unit
member ThreadState : System.Threading.ThreadState
member TrySetApartmentState : System.Threading.ApartmentState -> bool
static member AllocateDataSlot : unit -> System.LocalDataStoreSlot
static member AllocateNamedDataSlot : string -> System.LocalDataStoreSlot
static member BeginCriticalRegion : unit -> unit
static member BeginThreadAffinity : unit -> unit
static member CurrentContext : System.Runtime.Remoting.Contexts.Context
static member CurrentPrincipal : System.Security.Principal.IPrincipal with get, set
static member CurrentThread : System.Threading.Thread
static member EndCriticalRegion : unit -> unit
static member EndThreadAffinity : unit -> unit
static member FreeNamedDataSlot : string -> unit
static member GetData : System.LocalDataStoreSlot -> obj
static member GetDomain : unit -> System.AppDomain
static member GetDomainID : unit -> int
static member GetNamedDataSlot : string -> System.LocalDataStoreSlot
static member MemoryBarrier : unit -> unit
static member ResetAbort : unit -> unit
static member SetData : System.LocalDataStoreSlot * obj -> unit
static member Sleep : int -> unit
static member Sleep : System.TimeSpan -> unit
static member SpinWait : int -> unit
static member VolatileRead : System.Byte -> System.Byte
static member VolatileRead : int16 -> int16
static member VolatileRead : int -> int
static member VolatileRead : int64 -> int64
static member VolatileRead : System.SByte -> System.SByte
static member VolatileRead : uint16 -> uint16
static member VolatileRead : uint32 -> uint32
static member VolatileRead : System.IntPtr -> System.IntPtr
static member VolatileRead : System.UIntPtr -> System.UIntPtr
static member VolatileRead : uint64 -> uint64
static member VolatileRead : float32 -> float32
static member VolatileRead : float -> float
static member VolatileRead : obj -> obj
static member VolatileWrite : System.Byte * System.Byte -> unit
static member VolatileWrite : int16 * int16 -> unit
static member VolatileWrite : int * int -> unit
static member VolatileWrite : int64 * int64 -> unit
static member VolatileWrite : System.SByte * System.SByte -> unit
static member VolatileWrite : uint16 * uint16 -> unit
static member VolatileWrite : uint32 * uint32 -> unit
static member VolatileWrite : System.IntPtr * System.IntPtr -> unit
static member VolatileWrite : System.UIntPtr * System.UIntPtr -> unit
static member VolatileWrite : uint64 * uint64 -> unit
static member VolatileWrite : float32 * float32 -> unit
static member VolatileWrite : float * float -> unit
static member VolatileWrite : obj * obj -> unit
static member Yield : unit -> bool
end
Full name: System.Threading.Thread
type: Threading.Thread
implements: Runtime.InteropServices._Thread
inherits: Runtime.ConstrainedExecution.CriticalFinalizerObject
Threading.Thread.Sleep(millisecondsTimeout: int) : unit
Full name: RandomBits.next64
type: bool
implements: IComparable
implements: IConvertible
implements: IComparable<bool>
implements: IEquatable<bool>
inherits: ValueType
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
Full name: RandomBits.nextBit
Full name: RandomBits.unsignedOfBit
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
Full name: RandomBits.bitsToNumber
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
Full name: RandomBits.bitsToNumber2
Full name: RandomBits.randomWalk
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
Full name: RandomBits.rndInRange
Full name: RandomBits.rndSignInRange
Full name: Microsoft.FSharp.Core.Operators.invalidArg
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Full name: RandomBits.rndSeq
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int ref
implements: Collections.IStructuralEquatable
implements: IComparable<Ref<int>>
implements: IComparable
implements: Collections.IStructuralComparable
val ref : 'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
——————–
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
type: 'T ref
implements: Collections.IStructuralEquatable
implements: IComparable<Ref<'T>>
implements: IComparable
implements: Collections.IStructuralComparable
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
Full name: RandomBits.rndSignRangeSeq
Full name: RandomBits.rndUnsignRangeSeq
Full name: RandomBits.rndSignUniqueSeq
type: 'b ref
implements: Collections.IStructuralEquatable
implements: IComparable<Ref<'b>>
implements: IComparable
implements: Collections.IStructuralComparable
type: 'a ref
implements: Collections.IStructuralEquatable
implements: IComparable<Ref<'a>>
implements: IComparable
implements: Collections.IStructuralComparable
Full name: Microsoft.FSharp.Collections.Seq.fold
Full name: RandomBits.rndUnsignUniqueSeq
type: 'a ref
implements: Collections.IStructuralEquatable
implements: IComparable<Ref<'a>>
implements: IComparable
implements: Collections.IStructuralComparable
member private RandomBits.bigint : x:int64 -> BigInteger
Full name: RandomBits.bigint
——————–
type bigint = BigInteger
Full name: Microsoft.FSharp.Core.bigint
type: bigint
implements: IFormattable
implements: IComparable
implements: IComparable<BigInteger>
implements: IEquatable<BigInteger>
inherits: ValueType
type: int64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int64>
implements: IEquatable<int64>
inherits: ValueType
struct
new : int -> System.Numerics.BigInteger
new : uint32 -> System.Numerics.BigInteger
new : int64 -> System.Numerics.BigInteger
new : uint64 -> System.Numerics.BigInteger
new : float32 -> System.Numerics.BigInteger
new : float -> System.Numerics.BigInteger
new : decimal -> System.Numerics.BigInteger
new : System.Byte [] -> System.Numerics.BigInteger
member CompareTo : int64 -> int
member CompareTo : uint64 -> int
member CompareTo : System.Numerics.BigInteger -> int
member CompareTo : obj -> int
member Equals : obj -> bool
member Equals : int64 -> bool
member Equals : uint64 -> bool
member Equals : System.Numerics.BigInteger -> bool
member GetHashCode : unit -> int
member IsEven : bool
member IsOne : bool
member IsPowerOfTwo : bool
member IsZero : bool
member Sign : int
member ToByteArray : unit -> System.Byte []
member ToString : unit -> string
member ToString : System.IFormatProvider -> string
member ToString : string -> string
member ToString : string * System.IFormatProvider -> string
static member Abs : System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Add : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Compare : System.Numerics.BigInteger * System.Numerics.BigInteger -> int
static member DivRem : System.Numerics.BigInteger * System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Divide : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member GreatestCommonDivisor : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Log : System.Numerics.BigInteger -> float
static member Log : System.Numerics.BigInteger * float -> float
static member Log10 : System.Numerics.BigInteger -> float
static member Max : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Min : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member MinusOne : System.Numerics.BigInteger
static member ModPow : System.Numerics.BigInteger * System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Multiply : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Negate : System.Numerics.BigInteger -> System.Numerics.BigInteger
static member One : System.Numerics.BigInteger
static member Parse : string -> System.Numerics.BigInteger
static member Parse : string * System.Globalization.NumberStyles -> System.Numerics.BigInteger
static member Parse : string * System.IFormatProvider -> System.Numerics.BigInteger
static member Parse : string * System.Globalization.NumberStyles * System.IFormatProvider -> System.Numerics.BigInteger
static member Pow : System.Numerics.BigInteger * int -> System.Numerics.BigInteger
static member Remainder : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member Subtract : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
static member TryParse : string * System.Numerics.BigInteger -> bool
static member TryParse : string * System.Globalization.NumberStyles * System.IFormatProvider * System.Numerics.BigInteger -> bool
static member Zero : System.Numerics.BigInteger
end
Full name: System.Numerics.BigInteger
type: BigInteger
implements: IFormattable
implements: IComparable
implements: IComparable<BigInteger>
implements: IEquatable<BigInteger>
inherits: ValueType
Full name: RandomBits.bigintU
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
type: bool
implements: IComparable
implements: IConvertible
implements: IComparable<bool>
implements: IEquatable<bool>
inherits: ValueType
Full name: RandomBits.ANU_BlockCount
Full name: RandomBits.ANU_Url
Full name: RandomBits.CacheLength
Full name: RandomBits.Consume64Count
Full name: RandomBits.MaxCache
Full name: RandomBits.MaxPersistCache
Full name: RandomBits.PersistCacheLength
Full name: RandomBits.PersistPath
Full name: RandomBits.ReusePeristCache
Full name: RandomBits.RndBool
random bool
Full name: RandomBits.RndSByte
random signed 8-bit integer
Full name: Microsoft.FSharp.Core.Operators.sbyte
Full name: RandomBits.RndByte
random unsigned 8-bit integer
Full name: RandomBits.RndInt16
random signed 16-bit integer
Full name: Microsoft.FSharp.Core.Operators.int16
Full name: RandomBits.RndUint16
random unsigned 16-bit integer
Full name: RandomBits.RndInt32
random signed 32-bit integer
Full name: Microsoft.FSharp.Core.Operators.int32
Full name: RandomBits.RndUint32
random unsigned 32-bit integer
Full name: RandomBits.RndInt64
random signed 64-bit integer
Full name: RandomBits.RndUint64
random unsigned 64-bit integer
Full name: RandomBits.RndSByte
random signed 8-bit integer in range inclusive of lower and exclusive of upper
type: sbyte
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<sbyte>
implements: IEquatable<sbyte>
inherits: ValueType
type: sbyte
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<sbyte>
implements: IEquatable<sbyte>
inherits: ValueType
val byte : 'T -> byte (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.byte
——————–
type byte = Byte
Full name: Microsoft.FSharp.Core.byte
type: byte
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<byte>
implements: IEquatable<byte>
inherits: ValueType
random unsigned 8-bit integer
member private RandomBits.RndByte : inclLower:byte * exlUpper:byte -> byte
random unsigned 8-bit integer in range inclusive of lower and exclusive of upper
Full name: RandomBits.RndByte
random unsigned 8-bit integer in range inclusive of lower and exclusive of upper
type: byte
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<byte>
implements: IEquatable<byte>
inherits: ValueType
type: byte
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<byte>
implements: IEquatable<byte>
inherits: ValueType
Full name: RandomBits.RndInt16
random signed 16-bit integer in range inclusive of lower and exclusive of upper
type: int16
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int16>
implements: IEquatable<int16>
inherits: ValueType
type: int16
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int16>
implements: IEquatable<int16>
inherits: ValueType
val uint16 : 'T -> uint16 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint16
——————–
type uint16 = UInt16
Full name: Microsoft.FSharp.Core.uint16
type: uint16
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint16>
implements: IEquatable<uint16>
inherits: ValueType
random unsigned 16-bit integer
member private RandomBits.RndUint16 : inclLower:uint16 * exlUpper:uint16 -> uint16
random unsigned 16-bit integer in range inclusive of lower and exclusive of upper
Full name: RandomBits.RndUint16
random unsigned 16-bit integer in range inclusive of lower and exclusive of upper
type: uint16
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint16>
implements: IEquatable<uint16>
inherits: ValueType
type: uint16
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint16>
implements: IEquatable<uint16>
inherits: ValueType
Full name: RandomBits.RndInt32
random signed 32-bit integer in range inclusive of lower and exclusive of upper
type: int32
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int32
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
val uint32 : 'T -> uint32 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint32
——————–
type uint32 = UInt32
Full name: Microsoft.FSharp.Core.uint32
type: uint32
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint32>
implements: IEquatable<uint32>
inherits: ValueType
random unsigned 32-bit integer
member private RandomBits.RndUint32 : inclLower:uint32 * exlUpper:uint32 -> uint32
random unsigned 32-bit integer in range inclusive of lower and exclusive of upper
Full name: RandomBits.RndUint32
random unsigned 32-bit integer in range inclusive of lower and exclusive of upper
type: uint32
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint32>
implements: IEquatable<uint32>
inherits: ValueType
type: uint32
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint32>
implements: IEquatable<uint32>
inherits: ValueType
Full name: RandomBits.RndInt64
random signed 64-bit integer in range inclusive of lower and exclusive of upper
type: int64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int64>
implements: IEquatable<int64>
inherits: ValueType
type: int64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int64>
implements: IEquatable<int64>
inherits: ValueType
type: BigInteger
implements: IFormattable
implements: IComparable
implements: IComparable<BigInteger>
implements: IEquatable<BigInteger>
inherits: ValueType
type: BigInteger
implements: IFormattable
implements: IComparable
implements: IComparable<BigInteger>
implements: IEquatable<BigInteger>
inherits: ValueType
random unsigned 64-bit integer
member private RandomBits.RndUint64 : inclLower:uint64 * exlUpper:uint64 -> uint64
random unsigned 64-bit integer in range inclusive of lower and exclusive of upper
Full name: RandomBits.RndUint64
random unsigned 64-bit integer in range inclusive of lower and exclusive of upper
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
type: uint64
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<uint64>
implements: IEquatable<uint64>
inherits: ValueType
Full name: RandomBits.RndBoolSeq
random bool seq of length
Full name: RandomBits.RndSByteSeq
random signed 8-bit integer seq of length
Full name: RandomBits.RndByteSeq
random unsigned 8-bit integer seq of length
Full name: RandomBits.RndInt16Seq
random signed 16-bit integer seq of length
Full name: RandomBits.RndUint16Seq
random unsigned 16-bit integer seq of length
Full name: RandomBits.RndInt32Seq
random signed 32-bit integer seq of length
Full name: RandomBits.RndUint32Seq
random unsigned 32-bit integer seq of length
Full name: RandomBits.RndInt64Seq
random signed 64-bit integer seq of length
Full name: RandomBits.RndUint64Seq
random unsigned 64-bit integer seq of length
Full name: RandomBits.RndSByteSeq
random signed 8-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndByteSeq
random unsigned 8-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndInt16Seq
random signed 16-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndUint16Seq
random unsigned 16-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndInt32Seq
random usigned 32-bit integer in range inclusive of lower and exclusive of upper, seq of length
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
type: int
implements: IComparable
implements: IFormattable
implements: IConvertible
implements: IComparable<int>
implements: IEquatable<int>
inherits: ValueType
Full name: RandomBits.RndUint32Seq
random unsigned 32-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndInt64Seq
random usigned 64-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndUint64Seq
random unsigned 64-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndSByteUniqueSeq
random unique usigned 8-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndByteUniqueSeq
random unique unsigned 8-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndInt16UniqueSeq
random unique signed 16-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndUint16UniqueSeq
random unique unsigned 16-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndInt32UniqueSeq
random unique signed 32-bit integer in range inclusive of lower and exclusive of upper, seq of length
Full name: RandomBits.RndUint32UniqueSeq
random unique unsigned 32-bit integer in range inclusive of lower and exclusive of upper, seq of length
type IDisposable =
interface
member Dispose : unit -> unit
end
Full name: System.IDisposable
——————–
IDisposable
Full name: RandomBits.Dispose
And that’s almost all there is to generating random sequences of different types. In a future article I’ll show you the generic bit twiddling involved.
End Note: Formatted F# Snippets
As well applying my best engineering chops to the application, I’m taking this opportunity to better engineer this blog. For the first time I’ve formatted F# code snippets using Tomas Petricek’s FSharp.Formatting code formatter. Not only is the code color-coded like in an IDE, but mouse-over tool tips work just like intellisense.
FSharp.Formatting requires FSharp.CompilerBinding, then generating formatted F# with tool tip intellisense is as easy as running the following:
1: #r "FSharp.CodeFormat.dll" 2: open FSharp.CodeFormat 3: open System.IO 4: 5: let fsharpCompiler = "C:\\Program Files\\Microsoft F#\\v4.0\\FSharp.Compiler.dll" 6: let asm = System.Reflection.Assembly.LoadFile(fsharpCompiler) 7: let formatAgent = FSharp.CodeFormat.CodeFormatAgent(asm) 8: 9: let source = File.ReadAllText("C:\\Users\Jack\\Documents\\Scripts\\RandomBits.fs") 10: 11: let snippets, errors = formatAgent.ParseSource("C:\\Users\Jack\\Documents\\Scripts\\RandomBits.fs", source.Trim()) 12: let res = FSharp.CodeFormat.CodeFormat.FormatHtml(snippets, "fstipsA") 13: 14: let wr = new StreamWriter("C:\\Users\Jack\\Documents\\Scripts\\RandomBits.output") 15: for snip in res.SnippetsHtml do 16: wr.WriteLine(snip.Title) 17: wr.WriteLine(snip.Html + "\n") 18: wr.WriteLine("\n\n<!-- GENERATED TOOL TIPS -->") 19: wr.Write(res.ToolTipHtml) 20: wr.Dispose();;
from FSharp
Full name: FSharp.CodeFormat.fsharpCompiler
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable<string>
implements: seq<char>
implements: System.Collections.IEnumerable
implements: System.IEquatable<string>
Full name: FSharp.CodeFormat.asm
type: System.Reflection.Assembly
implements: System.Runtime.InteropServices._Assembly
implements: System.Security.IEvidenceFactory
implements: System.Reflection.ICustomAttributeProvider
implements: System.Runtime.Serialization.ISerializable
class
member CodeBase : string
member CreateInstance : string -> obj
member CreateInstance : string * bool -> obj
member CreateInstance : string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj [] * System.Globalization.CultureInfo * obj [] -> obj
member EntryPoint : System.Reflection.MethodInfo
member Equals : obj -> bool
member EscapedCodeBase : string
member Evidence : System.Security.Policy.Evidence
member FullName : string
member GetCustomAttributes : bool -> obj []
member GetCustomAttributes : System.Type * bool -> obj []
member GetCustomAttributesData : unit -> System.Collections.Generic.IList<System.Reflection.CustomAttributeData>
member GetExportedTypes : unit -> System.Type []
member GetFile : string -> System.IO.FileStream
member GetFiles : unit -> System.IO.FileStream []
member GetFiles : bool -> System.IO.FileStream []
member GetHashCode : unit -> int
member GetLoadedModules : unit -> System.Reflection.Module []
member GetLoadedModules : bool -> System.Reflection.Module []
member GetManifestResourceInfo : string -> System.Reflection.ManifestResourceInfo
member GetManifestResourceNames : unit -> string []
member GetManifestResourceStream : string -> System.IO.Stream
member GetManifestResourceStream : System.Type * string -> System.IO.Stream
member GetModule : string -> System.Reflection.Module
member GetModules : unit -> System.Reflection.Module []
member GetModules : bool -> System.Reflection.Module []
member GetName : unit -> System.Reflection.AssemblyName
member GetName : bool -> System.Reflection.AssemblyName
member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
member GetReferencedAssemblies : unit -> System.Reflection.AssemblyName []
member GetSatelliteAssembly : System.Globalization.CultureInfo -> System.Reflection.Assembly
member GetSatelliteAssembly : System.Globalization.CultureInfo * System.Version -> System.Reflection.Assembly
member GetType : string -> System.Type
member GetType : string * bool -> System.Type
member GetType : string * bool * bool -> System.Type
member GetTypes : unit -> System.Type []
member GlobalAssemblyCache : bool
member HostContext : int64
member ImageRuntimeVersion : string
member IsDefined : System.Type * bool -> bool
member IsDynamic : bool
member IsFullyTrusted : bool
member LoadModule : string * System.Byte [] -> System.Reflection.Module
member LoadModule : string * System.Byte [] * System.Byte [] -> System.Reflection.Module
member Location : string
member ManifestModule : System.Reflection.Module
member PermissionSet : System.Security.PermissionSet
member ReflectionOnly : bool
member SecurityRuleSet : System.Security.SecurityRuleSet
member ToString : unit -> string
static member CreateQualifiedName : string * string -> string
static member GetAssembly : System.Type -> System.Reflection.Assembly
static member GetCallingAssembly : unit -> System.Reflection.Assembly
static member GetEntryAssembly : unit -> System.Reflection.Assembly
static member GetExecutingAssembly : unit -> System.Reflection.Assembly
static member Load : string -> System.Reflection.Assembly
static member Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
static member Load : System.Byte [] -> System.Reflection.Assembly
static member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member Load : System.Byte [] * System.Byte [] -> System.Reflection.Assembly
static member Load : System.Byte [] * System.Byte [] * System.Security.SecurityContextSource -> System.Reflection.Assembly
static member Load : System.Byte [] * System.Byte [] * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member LoadFile : string -> System.Reflection.Assembly
static member LoadFile : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member LoadFrom : string -> System.Reflection.Assembly
static member LoadFrom : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member LoadFrom : string * System.Byte [] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
static member LoadFrom : string * System.Security.Policy.Evidence * System.Byte [] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
static member LoadWithPartialName : string -> System.Reflection.Assembly
static member LoadWithPartialName : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
static member ReflectionOnlyLoad : string -> System.Reflection.Assembly
static member ReflectionOnlyLoad : System.Byte [] -> System.Reflection.Assembly
static member ReflectionOnlyLoadFrom : string -> System.Reflection.Assembly
static member UnsafeLoadFrom : string -> System.Reflection.Assembly
end
Full name: System.Reflection.Assembly
type: System.Reflection.Assembly
implements: System.Runtime.InteropServices._Assembly
implements: System.Security.IEvidenceFactory
implements: System.Reflection.ICustomAttributeProvider
implements: System.Runtime.Serialization.ISerializable
Full name: FSharp.CodeFormat.formatAgent
class
new : fsharpCompiler:System.Reflection.Assembly -> CodeFormatAgent
member AsyncParseSource : file:string * source:string * ?options:string * ?defines:string -> Async<Snippet [] * SourceError []>
member ParseSource : file:string * source:string * ?options:string * ?defines:string -> Snippet [] * SourceError []
member ParseSourceAsync : file:string * source:string * options:string * defines:string -> System.Threading.Tasks.Task<Snippet [] * SourceError []>
end
Full name: FSharp.CodeFormat.CodeFormatAgent
Full name: FSharp.CodeFormat.source
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable<string>
implements: seq<char>
implements: System.Collections.IEnumerable
implements: System.IEquatable<string>
class
static member AppendAllLines : string * System.Collections.Generic.IEnumerable<string> -> unit
static member AppendAllLines : string * System.Collections.Generic.IEnumerable<string> * System.Text.Encoding -> unit
static member AppendAllText : string * string -> unit
static member AppendAllText : string * string * System.Text.Encoding -> unit
static member AppendText : string -> System.IO.StreamWriter
static member Copy : string * string -> unit
static member Copy : string * string * bool -> unit
static member Create : string -> System.IO.FileStream
static member Create : string * int -> System.IO.FileStream
static member Create : string * int * System.IO.FileOptions -> System.IO.FileStream
static member Create : string * int * System.IO.FileOptions * System.Security.AccessControl.FileSecurity -> System.IO.FileStream
static member CreateText : string -> System.IO.StreamWriter
static member Decrypt : string -> unit
static member Delete : string -> unit
static member Encrypt : string -> unit
static member Exists : string -> bool
static member GetAccessControl : string -> System.Security.AccessControl.FileSecurity
static member GetAccessControl : string * System.Security.AccessControl.AccessControlSections -> System.Security.AccessControl.FileSecurity
static member GetAttributes : string -> System.IO.FileAttributes
static member GetCreationTime : string -> System.DateTime
static member GetCreationTimeUtc : string -> System.DateTime
static member GetLastAccessTime : string -> System.DateTime
static member GetLastAccessTimeUtc : string -> System.DateTime
static member GetLastWriteTime : string -> System.DateTime
static member GetLastWriteTimeUtc : string -> System.DateTime
static member Move : string * string -> unit
static member Open : string * System.IO.FileMode -> System.IO.FileStream
static member Open : string * System.IO.FileMode * System.IO.FileAccess -> System.IO.FileStream
static member Open : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare -> System.IO.FileStream
static member OpenRead : string -> System.IO.FileStream
static member OpenText : string -> System.IO.StreamReader
static member OpenWrite : string -> System.IO.FileStream
static member ReadAllBytes : string -> System.Byte []
static member ReadAllLines : string -> string []
static member ReadAllLines : string * System.Text.Encoding -> string []
static member ReadAllText : string -> string
static member ReadAllText : string * System.Text.Encoding -> string
static member ReadLines : string -> System.Collections.Generic.IEnumerable<string>
static member ReadLines : string * System.Text.Encoding -> System.Collections.Generic.IEnumerable<string>
static member Replace : string * string * string -> unit
static member Replace : string * string * string * bool -> unit
static member SetAccessControl : string * System.Security.AccessControl.FileSecurity -> unit
static member SetAttributes : string * System.IO.FileAttributes -> unit
static member SetCreationTime : string * System.DateTime -> unit
static member SetCreationTimeUtc : string * System.DateTime -> unit
static member SetLastAccessTime : string * System.DateTime -> unit
static member SetLastAccessTimeUtc : string * System.DateTime -> unit
static member SetLastWriteTime : string * System.DateTime -> unit
static member SetLastWriteTimeUtc : string * System.DateTime -> unit
static member WriteAllBytes : string * System.Byte [] -> unit
static member WriteAllLines : string * string [] -> unit
static member WriteAllLines : string * System.Collections.Generic.IEnumerable<string> -> unit
static member WriteAllLines : string * string [] * System.Text.Encoding -> unit
static member WriteAllLines : string * System.Collections.Generic.IEnumerable<string> * System.Text.Encoding -> unit
static member WriteAllText : string * string -> unit
static member WriteAllText : string * string * System.Text.Encoding -> unit
end
Full name: System.IO.File
File.ReadAllText(path: string, encoding: System.Text.Encoding) : string
Full name: FSharp.CodeFormat.snippets
type: Snippet []
implements: System.ICloneable
implements: System.Collections.IList
implements: System.Collections.ICollection
implements: System.Collections.IStructuralComparable
implements: System.Collections.IStructuralEquatable
implements: System.Collections.Generic.IList<Snippet>
implements: System.Collections.Generic.ICollection<Snippet>
implements: seq<Snippet>
implements: System.Collections.IEnumerable
inherits: System.Array
Full name: FSharp.CodeFormat.errors
type: SourceError []
implements: System.ICloneable
implements: System.Collections.IList
implements: System.Collections.ICollection
implements: System.Collections.IStructuralComparable
implements: System.Collections.IStructuralEquatable
implements: System.Collections.Generic.IList<SourceError>
implements: System.Collections.Generic.ICollection<SourceError>
implements: seq<SourceError>
implements: System.Collections.IEnumerable
inherits: System.Array
System.String.Trim(trimChars: char []) : string
Full name: FSharp.CodeFormat.res
class
static member CreateAgent : fsharpCompiler:System.Reflection.Assembly -> CodeFormatAgent
static member FormatHtml : snippets:Snippet [] * prefix:string -> FormattedHtml
static member FormatHtml : snippets:Snippet [] * prefix:string * addLines:bool * addErrors:bool -> FormattedHtml
static member FormatHtml : snippets:Snippet [] * prefix:string * openTag:string * closeTag:string * addLines:bool * addErrors:bool -> FormattedHtml
end
Full name: FSharp.CodeFormat.CodeFormat
static member CodeFormat.FormatHtml : snippets:Snippet [] * prefix:string * addLines:bool * addErrors:bool -> FormattedHtml
static member CodeFormat.FormatHtml : snippets:Snippet [] * prefix:string * openTag:string * closeTag:string * addLines:bool * addErrors:bool -> FormattedHtml
Full name: FSharp.CodeFormat.wr
type: StreamWriter
implements: System.IDisposable
inherits: TextWriter
inherits: System.MarshalByRefObject
class
inherit System.IO.TextWriter
new : System.IO.Stream -> System.IO.StreamWriter
new : System.IO.Stream * System.Text.Encoding -> System.IO.StreamWriter
new : System.IO.Stream * System.Text.Encoding * int -> System.IO.StreamWriter
new : string -> System.IO.StreamWriter
new : string * bool -> System.IO.StreamWriter
new : string * bool * System.Text.Encoding -> System.IO.StreamWriter
new : string * bool * System.Text.Encoding * int -> System.IO.StreamWriter
member AutoFlush : bool with get, set
member BaseStream : System.IO.Stream
member Close : unit -> unit
member Encoding : System.Text.Encoding
member Flush : unit -> unit
member Write : char -> unit
member Write : char [] -> unit
member Write : string -> unit
member Write : char [] * int * int -> unit
static val Null : System.IO.StreamWriter
end
Full name: System.IO.StreamWriter
type: StreamWriter
implements: System.IDisposable
inherits: TextWriter
inherits: System.MarshalByRefObject
(+0 other overloads)
TextWriter.WriteLine(value: obj) : unit
(+0 other overloads)
TextWriter.WriteLine(value: string) : unit
(+0 other overloads)
TextWriter.WriteLine(value: decimal) : unit
(+0 other overloads)
TextWriter.WriteLine(value: float) : unit
(+0 other overloads)
TextWriter.WriteLine(value: float32) : unit
(+0 other overloads)
TextWriter.WriteLine(value: uint64) : unit
(+0 other overloads)
TextWriter.WriteLine(value: int64) : unit
(+0 other overloads)
TextWriter.WriteLine(value: uint32) : unit
(+0 other overloads)
TextWriter.WriteLine(value: int) : unit
(+0 other overloads)
(+0 other overloads)
TextWriter.Write(value: string) : unit
(+0 other overloads)
TextWriter.Write(value: decimal) : unit
(+0 other overloads)
TextWriter.Write(value: float) : unit
(+0 other overloads)
TextWriter.Write(value: float32) : unit
(+0 other overloads)
TextWriter.Write(value: uint64) : unit
(+0 other overloads)
TextWriter.Write(value: int64) : unit
(+0 other overloads)
TextWriter.Write(value: uint32) : unit
(+0 other overloads)
TextWriter.Write(value: int) : unit
(+0 other overloads)
TextWriter.Write(value: bool) : unit
(+0 other overloads)
The output required minimal editing for posting into WordPress. Don’t forget to add the FSharp.Formatting CSS and javascript to your site.
FSharp.Formatting is a great resource for promoting F# programming. Not only does it make your code examples look like they are in an IDE, but it makes them more informative. I encourage everyone posting F# articles to use this resource.
FSharp.Formatting Update
Tomas just released documentation updates to his project:
F# Formatting: Documentation tools
F# Formatting: Code formatting
Pingback: F# Weekly #52, 2012 – New Year Edition « Sergey Tihon's Blog
It’s very enlightening article. I came from a C# background and I trying to get closer to functional world (through F#).
Now F# is first .NET citizen and it’s not acceptable not to know it.
I love how this article because highlights the way F# can be expressive and concise; after all less code to maintain let you concentrate on real problems and let you craft more solid code.
I hope to see continuation and other article like this.
Regards, Giacomo