本文整理汇总了Golang中rand.NewSource函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSource函数的具体用法?Golang NewSource怎么用?Golang NewSource使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSource函数的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: LoadWorldStore
func LoadWorldStore(worldPath string) (world *WorldStore, err os.Error) {
levelData, err := loadLevelData(worldPath)
if err != nil {
return
}
// In both single-player and SMP maps, the 'spawn position' is stored in
// the level data.
x, xok := levelData.Lookup("Data/SpawnX").(*nbt.Int)
y, yok := levelData.Lookup("Data/SpawnY").(*nbt.Int)
z, zok := levelData.Lookup("Data/SpawnZ").(*nbt.Int)
if !xok || !yok || !zok {
err = os.NewError("Invalid map level data: does not contain Spawn{X,Y,Z}")
log.Printf("%#v", levelData)
return
}
spawnPosition := BlockXyz{
BlockCoord(x.Value),
BlockYCoord(y.Value),
BlockCoord(z.Value),
}
var timeTicks Ticks
if timeTag, ok := levelData.Lookup("Data/Time").(*nbt.Long); ok {
timeTicks = Ticks(timeTag.Value)
}
var chunkStores []chunkstore.IChunkStore
persistantChunkStore, err := chunkstore.ChunkStoreForLevel(worldPath, levelData, DimensionNormal)
if err != nil {
return
}
chunkStores = append(chunkStores, chunkstore.NewChunkService(persistantChunkStore))
var seed int64
if seedNbt, ok := levelData.Lookup("Data/RandomSeed").(*nbt.Long); ok {
seed = seedNbt.Value
} else {
seed = rand.NewSource(time.Seconds()).Int63()
}
chunkStores = append(chunkStores, chunkstore.NewChunkService(generation.NewTestGenerator(seed)))
for _, store := range chunkStores {
go store.Serve()
}
world = &WorldStore{
WorldPath: worldPath,
Seed: seed,
Time: timeTicks,
LevelData: levelData,
ChunkStore: chunkstore.NewChunkService(chunkstore.NewMultiStore(chunkStores)),
SpawnPosition: spawnPosition,
}
go world.ChunkStore.Serve()
return
}
示例3: 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)
}
}
示例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: Run
func (t RoundRobin) Run(array player.Array, match Match) []int {
rand := rand.New(rand.NewSource(time.Seconds()))
results := make([]int, array.Len())
wins := make([]int, array.Len())
// Every player plays every other once, sort players by number of wins
for a := 0; a < array.Len(); a++ {
for b := a + 1; b < array.Len(); b++ {
winner, _, _ := match.Play(a, b, array, rand)
wins[winner]++
}
}
for i := 0; i < len(results); i++ {
best := 0
for j := range wins {
if wins[j] > wins[best] {
best = j
}
}
results[i] = best
wins[best] = -1
}
return results
}
示例6: main
func main() {
api = twitter.NewApi()
done = make(chan bool)
r = rand.New(rand.NewSource(time.Seconds()))
crawl(kStart, 0)
<-done
}
示例7: NewGame
func NewGame(worldPath string) (game *Game, err os.Error) {
worldStore, err := worldstore.LoadWorldStore(worldPath)
if err != nil {
return nil, err
}
game = &Game{
players: make(map[EntityId]*player.Player),
playerNames: make(map[string]*player.Player),
workQueue: make(chan func(*Game), 256),
playerConnect: make(chan *player.Player),
playerDisconnect: make(chan EntityId),
time: worldStore.Time,
worldStore: worldStore,
}
game.entityManager.Init()
game.serverId = fmt.Sprintf("%016x", rand.NewSource(worldStore.Seed).Int63())
//game.serverId = "-"
game.shardManager = shardserver.NewLocalShardManager(worldStore.ChunkStore, &game.entityManager)
// TODO: Load the prefix from a config file
gamerules.CommandFramework = command.NewCommandFramework("/")
go game.mainLoop()
return
}
示例8: 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
}
}
}
}
示例9: 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
}
示例10: 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)
}
}
示例11: 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))
}
}
示例12: 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}
}
}
示例13: 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])
}
}
}
}
}
示例14: 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
}
示例15: 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))
}
}
}
}
}