Create Paste Log in Register
randline.go
Created 10 years ago by Kealper, with 994 total views.
The content of this paste is in Go.

package main

import (
	"fmt"
	"crypto/rand"
	"math/big"
	"io/ioutil"
	"os"
	"flag"
	"strings"
)

func main() {
	flag.Parse()
	file := flag.Arg(0)
	if file == "" {
		fmt.Println("Please enter a file name")
		return
	}
	fileData, err := ioutil.ReadFile(file)
	if err != nil {
		fmt.Println("Unable to read file")
		return
	}
	randSrc, hwerr := os.Open("/dev/hwrng")
	if hwerr != nil {
		fmt.Println("Error: Unable to open TRNG stream!")
		fmt.Println("Must be root!")
		return
	}
	fileLines := strings.Split(string(fileData), "\n")
	n, _ := rand.Int(randSrc, big.NewInt(int64(len(fileLines))))
	fmt.Println(fileLines[n.Int64()])
}

View the raw version of this paste.