LCG is missing a seed value #1

Closed
opened 2024-11-21 23:04:17 +00:00 by acidvegas · 1 comment

LCG is missing a seed value, meaning it is random, but the same random every time

You want either:

  1. a random seed every time so you can ensure the randomness is random each run
    or
  2. a definable seed for sharing to other machines for distribution
func LCG(cidr string, seed int64, out chan<- string) {
    // lcg constants
    const a uint64 = 1664525
    const c uint64 = 1013904223
    
    _, ipnet, _ := net.ParseCIDR(cidr)
    start := iptouint(ipnet.IP)
    ones, bits := ipnet.Mask.Size()
    addrcount := new(big.Int).Lsh(big.NewInt(1), uint(bits-ones))
    
    // Initialize with seed XORed with start address for better distribution
    x := uint64(seed) ^ uint64(start)
    m := uint64(addrcount.Uint64())
    
    for i := uint64(0); i < m; i++ {
        x = (a*x + c) % (1 << 32)  // mod of full 32bit addr count
        ip := toip(uint32((x % m) + uint64(start)))
        out <- ip
    }
}
LCG is missing a seed value, meaning it is random, but the same random every time You want either: 1. a random seed every time so you can ensure the randomness is random each run or 2. a definable seed for sharing to other machines for distribution ```go func LCG(cidr string, seed int64, out chan<- string) { // lcg constants const a uint64 = 1664525 const c uint64 = 1013904223 _, ipnet, _ := net.ParseCIDR(cidr) start := iptouint(ipnet.IP) ones, bits := ipnet.Mask.Size() addrcount := new(big.Int).Lsh(big.NewInt(1), uint(bits-ones)) // Initialize with seed XORed with start address for better distribution x := uint64(seed) ^ uint64(start) m := uint64(addrcount.Uint64()) for i := uint64(0); i < m; i++ { x = (a*x + c) % (1 << 32) // mod of full 32bit addr count ip := toip(uint32((x % m) + uint64(start))) out <- ip } } ```
acidvegas added the
enhancement
label 2024-11-21 23:04:17 +00:00
delorean was assigned by acidvegas 2024-11-21 23:04:17 +00:00
Owner

Thank you darling

Thank you darling
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: delorean/maraudir#1
No description provided.