From a344a9caaee9c369970841cef4f5af004107b5db Mon Sep 17 00:00:00 2001 From: delorean Date: Fri, 22 Nov 2024 17:45:33 -0600 Subject: [PATCH] lcg unique distribution improvement --- common/lcg.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/common/lcg.go b/common/lcg.go index 19835bf..246ec5c 100644 --- a/common/lcg.go +++ b/common/lcg.go @@ -1,6 +1,8 @@ package common import ( + "crypto/rand" + "encoding/binary" "fmt" "math/big" "net" @@ -19,6 +21,14 @@ func toip(num uint32) string { num&255) } +func seed() int64 { + var n int64 + if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil { + return 1337 + } + return n +} + // per-cidr linear congruential generator for efficient randomized target ip ordering, ty claude func LCG(cidr string, out chan<- string) { // lcg constants @@ -27,11 +37,10 @@ func LCG(cidr string, out chan<- string) { _, ipnet, _ := net.ParseCIDR(cidr) start := iptouint(ipnet.IP) - ones, bits := ipnet.Mask.Size() addrcount := new(big.Int).Lsh(big.NewInt(1), uint(bits-ones)) - x := uint64(start) + x := uint64(seed()) ^ uint64(start) // seed for unique randomization per execution m := uint64(addrcount.Uint64()) for i := uint64(0); i < m; i++ {