本文整理汇总了Golang中rand.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: init
func init() {
SyncChannel = make(chan float64, 100000)
fmt.Println(" SyncChannel created")
Rgen = rand.New(rand.NewSource(123813541954235))
Rgen2 = rand.New(rand.NewSource(12384235))
fmt.Println("init done")
// function called automatically on pacakge load, initializes system channels
// create channels
SystemChan = make([]*channel, NCh)
for i := range SystemChan {
c := new(channel)
c.i = i
c.Emitters = list.New()
c.Change = make(chan EmitterInt, 100)
go c.changeChan()
SystemChan[i] = c
}
// evaluate overlaping factors of channels
overlapN := int(math.Floor(1.0 / (1.0 - float64(roverlap))))
if overlapN < 1 {
overlapN = 1
}
if roverlap > 0.0 {
for i := NChRes; i < NCh; i++ {
SystemChan[i].coIntC = make([]coIntChan, overlapN*2)
for k := 1; k <= overlapN; k++ {
fac := 1.0 - float64(k)*(1.0-roverlap)
SystemChan[i].coIntC[overlapN-k].c = i - k
SystemChan[i].coIntC[overlapN-k].factor = float64(fac)
if i+k < NCh {
SystemChan[i].coIntC[overlapN+k-1].c = i + k
SystemChan[i].coIntC[overlapN+k-1].factor = float64(fac)
}
}
}
}
}
示例2: BenchmarkMonteFull
func BenchmarkMonteFull(b *testing.B) {
rng := rand.New(rand.NewSource(1))
start := torus.Location(136)
l := start
n := 0
tn := 0
s := 0
for i := 0; i < b.N*NMonte; i++ {
s++
var d maps.Direction
for _, d = range maps.Permute5(rng) {
if d == maps.NoMovement {
break
} else if nl := M.LocStep[l][d]; maps.StepableItem[M.Grid[nl]] {
l = nl
break
}
}
if l == 0 {
l = start
n++
tn += s
s = 0
}
}
if arenachatty {
log.Printf("mean steps to exit %.2f", float64(tn)/float64(n))
}
}
示例3: NewPerlinNoise
func NewPerlinNoise(seed int64) *PerlinNoise {
gen := &PerlinNoise{
seed: seed,
}
// The source's seed is reset to seed for each precomputed set so that any
// code reordering in this implementation does not alter the noise values
// produced for a given seed.
source := rand.NewSource(0)
rnd := rand.New(source)
// Initialize gen.permut.
source.Seed(seed)
perm := rnd.Perm(len(gen.permut))
for i := range perm {
gen.permut[i] = perm[i]
}
// Initialize gen.g2d.
source.Seed(seed)
for i := range perm {
randVector(gen.g2d[i][:], rnd)
normVector(gen.g2d[i][:])
}
return gen
}
示例4: fab
func (b *Bytes) fab(parent string) os.Error {
fh, err := os.OpenFile(parent, os.O_RDWR|os.O_APPEND, 0644)
if fh == nil {
return err
}
defer fh.Close()
rnd := rand.New(rand.NewSource(b.Seed))
for toWrite := b.Length; toWrite > 0; {
buf := &bytes.Buffer{}
for i := 0; i < CHUNKSIZE && toWrite > 0; i++ {
buf.WriteByte(byte(rnd.Int()))
toWrite--
}
_, err = buf.WriteTo(fh)
if err != nil {
return err
}
}
return nil
}
示例5: main
func main() {
api = twitter.NewApi()
done = make(chan bool)
r = rand.New(rand.NewSource(time.Seconds()))
crawl(kStart, 0)
<-done
}
示例6: initColors
func initColors(seed int64) {
fmt.Printf("%d ", seed)
r := rand.New(rand.NewSource(seed))
for i := 0; i < 100; i++ {
colors[i] = image.RGBAColor{uint8rand(r), uint8rand(r), uint8rand(r), 255}
}
}
示例7: Run
func (t SingleElimination) Run(array player.Array, match Match) []int {
rand := rand.New(rand.NewSource(time.Seconds()))
results := make([]int, array.Len())
num_ranked := len(results) - 1
rounds := make([][]int, intlog(array.Len())+1)
rounds[0] = initialSeeds(array.Len())
for r := 1; ; r++ {
if len(rounds[r-1])/2 == 0 {
break
}
rounds[r] = make([]int, len(rounds[r-1])/2)
for i := range rounds[r] {
a := rounds[r-1][2*i]
b := rounds[r-1][2*i+1]
rounds[r][i], results[num_ranked], _ = match.Play(a, b, array, rand)
num_ranked--
}
}
results[num_ranked] = rounds[len(rounds)-1][0]
if num_ranked != 0 {
panic("Didn't rank all the players!")
}
return results
}
示例8: TestMatrix3_MultiplyV
func TestMatrix3_MultiplyV(t *testing.T) {
const size = 3
r := rand.New(rand.NewSource(time.Nanoseconds()))
// Simple math test
for iterations := 0; iterations < 1000; iterations++ {
data1 := (Randf(r) - 0.5) * 1000
data2 := (Randf(r) - 0.5) * 1000
data3 := data1 * data2
for element := 0; element < size*size; element++ {
var m Matrix3
m[element] = data1
row_index := element / size
col_index := element % size
for v_element := 0; v_element < size; v_element++ {
var v Vector3
v[v_element] = data2
r1 := m.MultiplyV(v)
if col_index != v_element {
var r2 Vector3
if !r1.Equals(r2) {
t.Error("r1 != r2\n\tm:", m, "\n\r1:", r1, "\n\r2:", r2)
}
} else {
var r2 Vector3
r2[row_index] = data3
if !r1.Equals(r2) {
t.Error("r1 != r2\n\tm:", m, "\n\r1:", r1, "\n\r2:", r2)
}
}
}
}
}
// Test identity matrix
{
data := make([]float32, size)
m := MakeMatrix3Identity()
for iterations := 0; iterations < 1000; iterations++ {
for i := 0; i < size; i++ {
data[i] = (Randf(r) - 0.5) * 1000
}
v1 := MakeVector3(data...)
v2 := m.MultiplyV(v1)
if !v1.Equals(v2) {
t.Error("v1 != v2\n\tm:", m, "\n\v1:", v1, "\n\v2:", v2)
}
}
}
// TODO: Get a bunch of test data to do this
input_matrix_data := [...][size * size]float32{}
_ = input_matrix_data
}
示例9: TestMakeMatrix3
func TestMakeMatrix3(t *testing.T) {
const size = 3
r := rand.New(rand.NewSource(time.Nanoseconds()))
data := make([]float32, size*size)
for iterations := 0; iterations < 1000; iterations++ {
// Initialize the data
for i := 0; i < size*size; i++ {
data[i] = (Randf(r) - 0.5) * 1000
}
m1 := MakeMatrix3(true, data...)
m2 := MakeMatrix3(false, data...)
for r := 0; r < size; r++ {
for c := 0; c < size; c++ {
if m1.At(r, c) != data[r*size+c] {
t.Errorf("m[%d][%d] (%f) != data[%d] (%f)", r, c, m1.At(r, c), r*size+c, data[r*size+c])
}
if m2.At(r, c) != data[c*size+r] {
t.Errorf("m[%d][%d] (%f) != data[%d] (%f)", r, c, m1.At(r, c), r*size+c, data[r*size+c])
}
}
}
}
}
示例10: TestMatrix3_IdentityThis
func TestMatrix3_IdentityThis(t *testing.T) {
const size = 3
r := rand.New(rand.NewSource(time.Nanoseconds()))
data := make([]float32, size*size)
for iterations := 0; iterations < 1000; iterations++ {
// Initialize the data
for i := 0; i < size*size; i++ {
data[i] = (Randf(r) - 0.5) * 1000
}
m1 := MakeMatrix3(true, data...)
m1.IdentityThis()
for r := 0; r < size; r++ {
for c := 0; c < size; c++ {
if r == c {
if m1.At(r, c) != 1 {
t.Errorf("m[%d][%d] (%f) != 0", r, c, m1.At(r, c))
}
} else if m1.At(r, c) != 0 {
t.Errorf("m[%d][%d] (%f) != 0", r, c, m1.At(r, c))
}
}
}
}
}
示例11: TestMarshalUnmarshal
func TestMarshalUnmarshal(t *testing.T) {
rand := rand.New(rand.NewSource(0))
for i, iface := range messageTypes {
ty := reflect.ValueOf(iface).Type()
n := 100
if testing.Short() {
n = 5
}
for j := 0; j < n; j++ {
v, ok := quick.Value(ty, rand)
if !ok {
t.Errorf("#%d: failed to create value", i)
break
}
m1 := v.Elem().Interface()
m2 := iface
marshaled := marshal(msgIgnore, m1)
if err := unmarshal(m2, marshaled, msgIgnore); err != nil {
t.Errorf("#%d failed to unmarshal %#v: %s", i, m1, err)
break
}
if !reflect.DeepEqual(v.Interface(), m2) {
t.Errorf("#%d\ngot: %#v\nwant:%#v\n%x", i, m2, m1, marshaled)
break
}
}
}
}
示例12: BenchmarkEncodeRGBOpaque
func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(640, 480)
// Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds()
rnd := rand.New(rand.NewSource(123))
for y := bo.Min.Y; y < bo.Max.Y; y++ {
for x := bo.Min.X; x < bo.Max.X; x++ {
img.Set(x, y, image.RGBAColor{
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
255})
}
}
if !img.Opaque() {
panic("expected image to be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
options := &Options{Quality: 90}
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, options)
}
}
示例13: TestSum
func TestSum(t *testing.T) {
var buf [100000]uint8
rnd := rand.New(rand.NewSource(4))
for i := range buf {
buf[i] = uint8(rnd.Intn(256))
}
sum := func(offset, len int) uint32 {
rs := New()
for count := offset; count < len; count++ {
rs.Roll(buf[count])
}
return rs.Digest()
}
sum1a := sum(0, len(buf))
sum1b := sum(1, len(buf))
sum2a := sum(len(buf)-windowSize*5/2, len(buf)-windowSize)
sum2b := sum(0, len(buf)-windowSize)
sum3a := sum(0, windowSize+3)
sum3b := sum(3, windowSize+3)
if sum1a != sum1b {
t.Errorf("sum1a=%d sum1b=%d", sum1a, sum1b)
}
if sum2a != sum2b {
t.Errorf("sum2a=%d sum2b=%d", sum2a, sum2b)
}
if sum3a != sum3b {
t.Errorf("sum3a=%d sum3b=%d", sum3a, sum3b)
}
}
示例14: BenchmarkMonte
func BenchmarkMonte(b *testing.B) {
rng := rand.New(rand.NewSource(1))
start := ALoc(40)
l := start
n := 0
tn := 0
s := 0
for i := 0; i < b.N*NMonte; i++ {
var d maps.Direction
for _, d = range maps.Permute5(rng) {
s++
if d == maps.NoMovement {
break
} else if nl := A.LocStep[l][d]; maps.StepableItem[A.Grid[nl]] {
l = nl
break
}
}
if l == 0 {
l = start
n++
tn += s
s = 0
}
}
if arenachatty {
log.Printf("mean steps to exit %.2f exited %d Steps %d %d", float64(tn)/float64(n), n, tn, b.N)
}
}
示例15: main
func main() {
src, _ := filesource.NewFileBasedSource("/dev/random")
rgn := rand.New(src)
var sum int64 = 0
for i := 0; i < 5000000; i++ {
sum += int64(rgn.Int31())
}
fmt.Printf("Hello, world. Random average sum is %f \n", sum/5000000)
}