Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 1.46 KB

README.md

File metadata and controls

43 lines (33 loc) · 1.46 KB

randomSum

Build Status GoDoc Go Report Card Codecov

Package randomSum provides a function for generating random slices of ints that sum to a specified value.

Example

package main

import (
	"fmt"
	"github.com/pdbrito/randomSum"
)

func main() {
	ints := randomSum.NIntsTotaling(20, 1000)
	fmt.Printf("ints: %v\n", ints)
	fmt.Printf("The length of ints is %v\n", len(ints))
	fmt.Printf("The sum of ints is %v\n", sum(ints))

	// ints: [42 862 12 56 2 5 4 1 1 1 3 2 1 1 1 1 1 1 1 2]
	// The length of ints is 20
	// The sum of ints is 1000
}

func sum(ints []int) int {
	sum := 0
	for _, value := range ints {
		sum += value
	}
	return sum
}

Seeding for random values

randomSum relies on the rand package to generate values for the ints that it returns.

By default this is deterministic and can only be changed by setting a seed before calling the NIntsTotalling function.

E.g. rand.Seed(time.Now().UnixNano())