Skip to content

Commit

Permalink
Add Random function
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jul 1, 2023
1 parent 19037f3 commit 065d32e
Show file tree
Hide file tree
Showing 3 changed files with 662 additions and 160 deletions.
47 changes: 47 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package trit

import (
"context"
"math/rand"
"reflect"
"runtime"
"sync"
"time"
)

var (
Expand Down Expand Up @@ -617,3 +619,48 @@ func IsConfidence[T Logicable](ts ...T) bool {

return true
}

// Random returns a random Trit value.
// The function can accept an optional argument that indicates the
// percentage probability of the occurrence of the Unknown event.
//
// Example usage:
//
// a := trit.Random()
// fmt.Println(a.String()) // Output: True, False or Unknown
//
// b := trit.Random(0)
// fmt.Println(b.String()) // Output: True or False
//
// c := trit.Random(50)
// fmt.Println(c.String()) // Output: With a probability of 50% is Unknown
func Random(up ...uint8) Trit {
// Determination of the probability of occurrence of the event Unknown.
var p int

if len(up) == 0 {
p = 33
} else {
for _, v := range up {
p += int(v)
}
}

if p > 100 {
p = 100
}

// Generate random value.
rand.Seed(time.Now().UnixNano())
value := rand.Intn(100)

if value < p {
return Unknown
}

if value < (100-p)/2 {
return True
}

return False
}
Loading

0 comments on commit 065d32e

Please sign in to comment.