Skip to content

Commit

Permalink
all: replace publicly facing *rand.Rand with rand.Source
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed May 1, 2018
1 parent 368b432 commit daf15bb
Show file tree
Hide file tree
Showing 39 changed files with 157 additions and 132 deletions.
4 changes: 2 additions & 2 deletions graph/community/bisect.go
Expand Up @@ -62,7 +62,7 @@ func Weight(g ReducedGraph) float64 {
// ModularScore returns a modularized scoring function for Profile based on the
// graph g and the given score function. The effort parameter determines how
// many attempts will be made to get an improved score for any given resolution.
func ModularScore(g graph.Graph, score func(ReducedGraph) float64, effort int, src *rand.Rand) func(float64) (float64, Reduced) {
func ModularScore(g graph.Graph, score func(ReducedGraph) float64, effort int, src rand.Source) func(float64) (float64, Reduced) {
return func(resolution float64) (float64, Reduced) {
max := math.Inf(-1)
var best Reduced
Expand Down Expand Up @@ -108,7 +108,7 @@ func WeightMultiplex(g ReducedMultiplex) float64 {
// ModularMultiplexScore returns a modularized scoring function for Profile based
// on the graph g and the given score function. The effort parameter determines how
// many attempts will be made to get an improved score for any given resolution.
func ModularMultiplexScore(g Multiplex, weights []float64, all bool, score func(ReducedMultiplex) float64, effort int, src *rand.Rand) func(float64) (float64, Reduced) {
func ModularMultiplexScore(g Multiplex, weights []float64, all bool, score func(ReducedMultiplex) float64, effort int, src rand.Source) func(float64) (float64, Reduced) {
return func(resolution float64) (float64, Reduced) {
max := math.Inf(-1)
var best Reduced
Expand Down
4 changes: 2 additions & 2 deletions graph/community/louvain_common.go
Expand Up @@ -82,7 +82,7 @@ type ReducedGraph interface {
//
// graph.Undirect may be used as a shim to allow modularization of
// directed graphs with the undirected modularity function.
func Modularize(g graph.Graph, resolution float64, src *rand.Rand) ReducedGraph {
func Modularize(g graph.Graph, resolution float64, src rand.Source) ReducedGraph {
switch g := g.(type) {
case graph.Undirected:
return louvainUndirected(g, resolution, src)
Expand Down Expand Up @@ -192,7 +192,7 @@ type ReducedMultiplex interface {
//
// graph.Undirect may be used as a shim to allow modularization of
// directed graphs with the undirected modularity function.
func ModularizeMultiplex(g Multiplex, weights, resolutions []float64, all bool, src *rand.Rand) ReducedMultiplex {
func ModularizeMultiplex(g Multiplex, weights, resolutions []float64, all bool, src rand.Source) ReducedMultiplex {
if weights != nil && len(weights) != g.Depth() {
panic("community: weights vector length mismatch")
}
Expand Down
4 changes: 2 additions & 2 deletions graph/community/louvain_directed.go
Expand Up @@ -79,14 +79,14 @@ func qDirected(g graph.Directed, communities [][]graph.Node, resolution float64)
// resolution using the Louvain algorithm. If src is nil, rand.Intn is used
// as the random generator. louvainDirected will panic if g has any edge with negative
// edge weight.
func louvainDirected(g graph.Directed, resolution float64, src *rand.Rand) ReducedGraph {
func louvainDirected(g graph.Directed, resolution float64, src rand.Source) ReducedGraph {
// See louvain.tex for a detailed description
// of the algorithm used here.

c := reduceDirected(g, nil)
rnd := rand.Intn
if src != nil {
rnd = src.Intn
rnd = rand.New(src).Intn
}
for {
l := newDirectedLocalMover(c, c.communities, resolution)
Expand Down
4 changes: 2 additions & 2 deletions graph/community/louvain_directed_multiplex.go
Expand Up @@ -168,7 +168,7 @@ func (g DirectedLayers) Layer(l int) graph.Directed { return g[l] }
// edge weight that does not sign-match the layer weight.
//
// graph.Undirect may be used as a shim to allow modularization of directed graphs.
func louvainDirectedMultiplex(g DirectedMultiplex, weights, resolutions []float64, all bool, src *rand.Rand) *ReducedDirectedMultiplex {
func louvainDirectedMultiplex(g DirectedMultiplex, weights, resolutions []float64, all bool, src rand.Source) *ReducedDirectedMultiplex {
if weights != nil && len(weights) != g.Depth() {
panic("community: weights vector length mismatch")
}
Expand All @@ -182,7 +182,7 @@ func louvainDirectedMultiplex(g DirectedMultiplex, weights, resolutions []float6
c := reduceDirectedMultiplex(g, nil, weights)
rnd := rand.Intn
if src != nil {
rnd = src.Intn
rnd = rand.New(src).Intn
}
for {
l := newDirectedMultiplexLocalMover(c, c.communities, weights, resolutions, all)
Expand Down
4 changes: 2 additions & 2 deletions graph/community/louvain_undirected.go
Expand Up @@ -77,14 +77,14 @@ func qUndirected(g graph.Undirected, communities [][]graph.Node, resolution floa
// weight.
//
// graph.Undirect may be used as a shim to allow modularization of directed graphs.
func louvainUndirected(g graph.Undirected, resolution float64, src *rand.Rand) *ReducedUndirected {
func louvainUndirected(g graph.Undirected, resolution float64, src rand.Source) *ReducedUndirected {
// See louvain.tex for a detailed description
// of the algorithm used here.

c := reduceUndirected(g, nil)
rnd := rand.Intn
if src != nil {
rnd = src.Intn
rnd = rand.New(src).Intn
}
for {
l := newUndirectedLocalMover(c, c.communities, resolution)
Expand Down
4 changes: 2 additions & 2 deletions graph/community/louvain_undirected_multiplex.go
Expand Up @@ -165,7 +165,7 @@ func (g UndirectedLayers) Layer(l int) graph.Undirected { return g[l] }
// edge weight that does not sign-match the layer weight.
//
// graph.Undirect may be used as a shim to allow modularization of directed graphs.
func louvainUndirectedMultiplex(g UndirectedMultiplex, weights, resolutions []float64, all bool, src *rand.Rand) *ReducedUndirectedMultiplex {
func louvainUndirectedMultiplex(g UndirectedMultiplex, weights, resolutions []float64, all bool, src rand.Source) *ReducedUndirectedMultiplex {
if weights != nil && len(weights) != g.Depth() {
panic("community: weights vector length mismatch")
}
Expand All @@ -179,7 +179,7 @@ func louvainUndirectedMultiplex(g UndirectedMultiplex, weights, resolutions []fl
c := reduceUndirectedMultiplex(g, nil, weights)
rnd := rand.Intn
if src != nil {
rnd = src.Intn
rnd = rand.New(src).Intn
}
for {
l := newUndirectedMultiplexLocalMover(c, c.communities, weights, resolutions, all)
Expand Down
23 changes: 12 additions & 11 deletions graph/graphs/gen/batagelj_brandes.go
Expand Up @@ -21,7 +21,7 @@ import (
// between nodes are formed with the probability, p. If src is not nil it is used
// as the random source, otherwise rand.Float64 is used. The graph is constructed
// in O(n+m) time where m is the number of edges added.
func Gnp(dst GraphBuilder, n int, p float64, src *rand.Rand) error {
func Gnp(dst GraphBuilder, n int, p float64, src rand.Source) error {
if p == 0 {
return nil
}
Expand All @@ -32,7 +32,7 @@ func Gnp(dst GraphBuilder, n int, p float64, src *rand.Rand) error {
if src == nil {
r = rand.Float64
} else {
r = src.Float64
r = rand.New(src).Float64
}

for i := 0; i < n; i++ {
Expand Down Expand Up @@ -87,7 +87,7 @@ func edgeNodesFor(i int) (v, w simple.Node) {
// order n and size m. If src is not nil it is used as the random source,
// otherwise rand.Intn is used. The graph is constructed in O(m) expected
// time for m ≤ (n choose 2)/2.
func Gnm(dst GraphBuilder, n, m int, src *rand.Rand) error {
func Gnm(dst GraphBuilder, n, m int, src rand.Source) error {
if m == 0 {
return nil
}
Expand All @@ -108,7 +108,7 @@ func Gnm(dst GraphBuilder, n, m int, src *rand.Rand) error {
if src == nil {
rnd = rand.Intn
} else {
rnd = src.Intn
rnd = rand.New(src).Intn
}

for i := 0; i < n; i++ {
Expand Down Expand Up @@ -153,7 +153,7 @@ func Gnm(dst GraphBuilder, n, m int, src *rand.Rand) error {
// The graph is constructed in O(nd) time.
//
// The algorithm used is described in http://algo.uni-konstanz.de/publications/bb-eglrn-05.pdf
func SmallWorldsBB(dst GraphBuilder, n, d int, p float64, src *rand.Rand) error {
func SmallWorldsBB(dst GraphBuilder, n, d int, p float64, src rand.Source) error {
if d < 1 || d > (n-1)/2 {
return fmt.Errorf("gen: bad degree: d=%d", d)
}
Expand All @@ -171,8 +171,9 @@ func SmallWorldsBB(dst GraphBuilder, n, d int, p float64, src *rand.Rand) error
rnd = rand.Float64
rndN = rand.Intn
} else {
rnd = src.Float64
rndN = src.Intn
r := rand.New(src)
rnd = r.Float64
rndN = r.Intn
}

hasEdge := dst.HasEdgeBetween
Expand Down Expand Up @@ -298,15 +299,15 @@ func SmallWorldsBB(dst GraphBuilder, n, d int, p float64, src *rand.Rand) error
// The graph is constructed in O(nd) — O(n+m) — time.
//
// The algorithm used is described in http://algo.uni-konstanz.de/publications/bb-eglrn-05.pdf
func PowerLaw(dst graph.MultigraphBuilder, n, d int, src *rand.Rand) error {
func PowerLaw(dst graph.MultigraphBuilder, n, d int, src rand.Source) error {
if d < 1 {
return fmt.Errorf("gen: bad minimum degree: d=%d", d)
}
var rnd func(int) int
if src == nil {
rnd = rand.Intn
} else {
rnd = src.Intn
rnd = rand.New(src).Intn
}

m := make([]graph.Node, 2*n*d)
Expand All @@ -333,15 +334,15 @@ func PowerLaw(dst graph.MultigraphBuilder, n, d int, src *rand.Rand) error {
// The graph is constructed in O(nd) — O(n+m) — time.
//
// The algorithm used is described in http://algo.uni-konstanz.de/publications/bb-eglrn-05.pdf
func BipartitePowerLaw(dst graph.MultigraphBuilder, n, d int, src *rand.Rand) (p1, p2 []graph.Node, err error) {
func BipartitePowerLaw(dst graph.MultigraphBuilder, n, d int, src rand.Source) (p1, p2 []graph.Node, err error) {
if d < 1 {
return nil, nil, fmt.Errorf("gen: bad minimum degree: d=%d", d)
}
var rnd func(int) int
if src == nil {
rnd = rand.Intn
} else {
rnd = src.Intn
rnd = rand.New(src).Intn
}

p := make([]graph.Node, 2*n)
Expand Down
7 changes: 4 additions & 3 deletions graph/graphs/gen/duplication.go
Expand Up @@ -29,7 +29,7 @@ type UndirectedMutator interface {
// created with probability sigma. With the exception of the sigma parameter, this
// corresponds to the completely correlated case in doi:10.1016/S0022-5193(03)00028-6.
// If src is not nil it is used as the random source, otherwise rand.Float64 is used.
func Duplication(dst UndirectedMutator, n int, delta, alpha, sigma float64, src *rand.Rand) error {
func Duplication(dst UndirectedMutator, n int, delta, alpha, sigma float64, src rand.Source) error {
// As described in doi:10.1016/S0022-5193(03)00028-6 but
// also clarified in doi:10.1186/gb-2007-8-4-r51.

Expand All @@ -51,8 +51,9 @@ func Duplication(dst UndirectedMutator, n int, delta, alpha, sigma float64, src
rnd = rand.Float64
rndN = rand.Intn
} else {
rnd = src.Float64
rndN = src.Intn
r := rand.New(src)
rnd = r.Float64
rndN = r.Intn
}

nodes := dst.Nodes()
Expand Down
13 changes: 7 additions & 6 deletions graph/graphs/gen/holme_kim.go
Expand Up @@ -21,10 +21,10 @@ import (
// additional edges joining existing nodes with probability proportional to the nodes'
// degrees. The edges are formed as a triad with probability, p.
// If src is not nil it is used as the random source, otherwise rand.Float64 and
// rand.Intn are used.
// rand.Intn are used for the random number generators.
//
// The algorithm is essentially as described in http://arxiv.org/abs/cond-mat/0110452.
func TunableClusteringScaleFree(dst graph.UndirectedBuilder, n, m int, p float64, src *rand.Rand) error {
func TunableClusteringScaleFree(dst graph.UndirectedBuilder, n, m int, p float64, src rand.Source) error {
if p < 0 || p > 1 {
return fmt.Errorf("gen: bad probability: p=%v", p)
}
Expand All @@ -40,8 +40,9 @@ func TunableClusteringScaleFree(dst graph.UndirectedBuilder, n, m int, p float64
rnd = rand.Float64
rndN = rand.Intn
} else {
rnd = src.Float64
rndN = src.Intn
r := rand.New(src)
rnd = r.Float64
rndN = r.Intn
}

// Initial condition.
Expand Down Expand Up @@ -116,11 +117,11 @@ func permute(n []graph.Node, rnd func(int) int) []graph.Node {
// node having degree m-1. At each iteration of graph addition, one node is added
// with m additional edges joining existing nodes with probability proportional
// to the nodes' degrees. If src is not nil it is used as the random source,
// otherwise rand.Float64 is used.
// otherwise rand.Float64 is for the random number generator.
//
// The algorithm is essentially as described in http://arxiv.org/abs/cond-mat/0110452
// after 10.1126/science.286.5439.509.
func PreferentialAttachment(dst graph.UndirectedBuilder, n, m int, src *rand.Rand) error {
func PreferentialAttachment(dst graph.UndirectedBuilder, n, m int, src rand.Source) error {
if n <= m {
return fmt.Errorf("gen: n <= m: n=%v m=%d", n, m)
}
Expand Down
2 changes: 1 addition & 1 deletion graph/graphs/gen/small_world.go
Expand Up @@ -24,7 +24,7 @@ import (
// Manhattan distance between non-local nodes.
//
// The algorithm is essentially as described on p4 of http://www.cs.cornell.edu/home/kleinber/swn.pdf.
func NavigableSmallWorld(dst GraphBuilder, dims []int, p, q int, r float64, src *rand.Rand) (err error) {
func NavigableSmallWorld(dst GraphBuilder, dims []int, p, q int, r float64, src rand.Source) (err error) {
if p < 1 {
return fmt.Errorf("gen: bad local distance: p=%v", p)
}
Expand Down
2 changes: 1 addition & 1 deletion optimize/cmaes.go
Expand Up @@ -82,7 +82,7 @@ type CmaEsChol struct {
ForgetBest bool
// Src allows a random number generator to be supplied for generating samples.
// If Src is nil the generator in golang.org/x/math/rand is used.
Src *rand.Rand
Src rand.Source

// Fixed algorithm parameters.
dim int
Expand Down
4 changes: 2 additions & 2 deletions stat/distmat/wishart.go
Expand Up @@ -27,7 +27,7 @@ import (
// See https://en.wikipedia.org/wiki/Wishart_distribution for more information.
type Wishart struct {
nu float64
src *rand.Rand
src rand.Source

dim int
cholv mat.Cholesky
Expand All @@ -43,7 +43,7 @@ type Wishart struct {
// successful.
//
// NewWishart panics if nu <= d - 1 where d is the order of v.
func NewWishart(v mat.Symmetric, nu float64, src *rand.Rand) (*Wishart, bool) {
func NewWishart(v mat.Symmetric, nu float64, src rand.Source) (*Wishart, bool) {
dim := v.Symmetric()
if nu <= float64(dim-1) {
panic("wishart: nu must be greater than dim-1")
Expand Down
4 changes: 2 additions & 2 deletions stat/distmv/dirichlet.go
Expand Up @@ -27,15 +27,15 @@ import (
type Dirichlet struct {
alpha []float64
dim int
src *rand.Rand
src rand.Source

lbeta float64
sumAlpha float64
}

// NewDirichlet creates a new dirichlet distribution with the given parameters alpha.
// NewDirichlet will panic if len(alpha) == 0, or if any alpha is <= 0.
func NewDirichlet(alpha []float64, src *rand.Rand) *Dirichlet {
func NewDirichlet(alpha []float64, src rand.Source) *Dirichlet {
dim := len(alpha)
if dim == 0 {
panic(badZeroDimension)
Expand Down

0 comments on commit daf15bb

Please sign in to comment.