Skip to content

pdbrito/randomSum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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())

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages