Skip to content

Instantly share code, notes, and snippets.

@ewrvp7lv7
Last active November 20, 2021 15:54
Show Gist options
  • Save ewrvp7lv7/a94688f3dd53e3fd3964e38d69e30828 to your computer and use it in GitHub Desktop.
Save ewrvp7lv7/a94688f3dd53e3fd3964e38d69e30828 to your computer and use it in GitHub Desktop.
Dont use rand.Intn(int)!

Don't use Pseudorandom number generator rand.Intn(int)!

Each time you set the same seed, you get the same sequence. stackoverflow

package main

import (
	"encoding/gob"
	"fmt"
	"math/rand"
	"os"
	"time"
)

func main() {

	//*if we seed with rand.Seed(time.Now().UnixNano())
	//and then another initializer calls rand.Seed(1)
	//our seed will get overridden, and that definitely isn’t what we want
	//fmt.Println(customRand.Intn(100))
	arg := os.Args[1]

	ai := []int{} //array former data 4 compare
	ao := []int{} //array for new data
	c := 0

    //It gets data array from file with custom prefix and compares with current data array.
	file, err := os.OpenFile(arg+"a.gob", os.O_RDWR, 0644)
	if err != nil {
		fmt.Println("will create new file")
	} else {
		decoder := gob.NewDecoder(file)
		err := decoder.Decode(&ai)
		if err != nil {
			fmt.Printf("decoder ", err)
		}
	}
	defer	file.Close()

	customSource := rand.NewSource(time.Now().UnixNano())
	customRand := rand.New(customSource)

	for i := 0; i < 1000; i++ {
		rnd := 0
		switch arg {
		case "0":
			rnd = rand.Intn(100) //don't use!
		case "1":
			rnd = customRand.Intn(100)
		case "2":
			customSource2 := rand.NewSource(time.Now().UnixNano())
			customRand2 := rand.New(customSource2)
			rnd = customRand2.Intn(100)
		default:
			rand.Seed(time.Now().UnixNano())
			rnd = rand.Intn(100)
		}

		ao = append(ao, rnd)

		if len(ai) > 0 {
			//fmt.Printf("%3d, %5d\n", ai[i], rnd)

			if rnd != ai[i] {
				c++
			}
		}

	}

	fmt.Println("not match:", c)

	file, _ = os.Create(arg + "a.gob")
	encoder := gob.NewEncoder(file)
	err = encoder.Encode(ao)
	if err != nil {
		fmt.Printf("encoder ", err)
	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment