本文整理汇总了Golang中math/rand.Float32函数的典型用法代码示例。如果您正苦于以下问题:Golang Float32函数的具体用法?Golang Float32怎么用?Golang Float32使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Float32函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: merge
func merge(string1 string, string2 string) string {
if len(string1) == 0 {
return string2
}
if len(string2) == 0 {
return string1
}
if len(string1) == 1 && len(string2) == 1 {
if rand.Float32() < .5 {
return string1 + string2
}
return string2 + string1
}
for i := 0; i < len(string1); i++ {
for j := 0; j < len(string2); j++ {
if string1[i] == string2[j] && rand.Float32() < mergesame {
return merge(string1[:i], string2[:j]) + string(string1[i]) + merge(string1[i+1:], string2[j+1:])
}
}
}
a := rand.Int() % len(string1)
b := rand.Int() % len(string2)
return merge(string1[:a], string2[:b]) + merge(string1[a:], string2[b:])
}
示例2: RandLen
func RandLen(x float32) XYZ {
return XYZ{
X: rand.Float32() - 1/2,
Y: rand.Float32() - 1/2,
Z: rand.Float32() - 1/2,
}.Norm().Mul(x)
}
示例3: OnDie
func (sp *EnemeyAI) OnDie(byTimer bool) {
sxps := 4
size := float32(0.5)
if sp.Type == Enemey_Boss {
sxps = 10
size = 3
Wall.Destroy()
queenDead = true
} else {
CreatePowerUp(sp.Transform().WorldPosition())
}
for i := 0; i < sxps; i++ {
n := Explosion.Clone()
n.Transform().SetParent2(GameSceneGeneral.Layer1)
n.Transform().SetWorldPosition(sp.Transform().WorldPosition())
s := n.Transform().Scale()
n.Transform().SetScale(s.Mul2((rand.Float32() * 3) + size))
n.AddComponent(engine.NewPhysics(false))
n.Transform().SetRotationf(rand.Float32() * 360)
rot := n.Transform().Direction()
n.Physics.Body.SetVelocity(-rot.X*15, -rot.Y*15)
n.Physics.Body.SetMass(1)
n.Physics.Shape.Group = 1
n.Physics.Shape.IsSensor = true
}
sp.GameObject().Destroy()
}
示例4: Rand
func Rand(s float32) XYZ {
return XYZ{
X: rand.Float32()*s - s/2,
Y: rand.Float32()*s - s/2,
Z: rand.Float32()*s - s/2,
}
}
示例5: TestMinPHeap
func TestMinPHeap(t *testing.T) {
Convey("Test that output after random input is sorted.", t, func() {
m := New()
for i := 0; i < 1000; i++ {
m.Insert(i, rand.Float32())
}
_, prev := m.Pop()
for i := 0; i < 999; i++ {
_, key := m.Pop()
So(key, ShouldBeGreaterThanOrEqualTo, prev)
prev = key
}
})
Convey("Test DecreaseKey.", t, func() {
m := New()
for i := 0; i < 1000; i++ {
m.Insert(i, rand.Float32())
}
for i := 0; i < 1000; i++ {
val, _ := m.PeekAtVal(i)
m.DecreaseKey(i, val-1)
}
_, prev := m.Pop()
for i := 0; i < 999; i++ {
_, key := m.Pop()
So(key, ShouldBeGreaterThanOrEqualTo, prev)
prev = key
}
})
}
示例6: init
func init() {
rand.Seed(4)
// create random samples
fmt.Printf("generating %d random %d-D test samples...\n", N, D)
samples := make([]point, N)
for i := range samples {
samples[i] = make([]float32, D)
for d := 0; d < D; d++ {
samples[i][d] = rand.Float32()
}
}
// make interfaced samples
isamples = make([]Point, len(samples))
for i := range isamples {
isamples[i] = &samples[i]
}
// create random queries
fmt.Printf("generating %d random %d-D test queries...\n", N, D)
queries := make([]point, N)
for i := range queries {
queries[i] = make([]float32, D)
for d := 0; d < D; d++ {
queries[i][d] = rand.Float32()
}
}
// make interfaced queries
iqueries = make([]Point, len(queries))
for i := range iqueries {
iqueries[i] = &queries[i]
}
}
示例7: createPhoto
// createPhoto looks up or creates a new user to match userID (it's
// the only method in this interface which doesn't match an existing
// user except for createUser). It then creates a new photo for the
// new or pre-existing user.
func createPhoto(tx *sql.Tx, userID int) error {
if err := createUser(tx, userID); err != nil {
return err
}
const insertSQL = `
INSERT INTO photos VALUES (DEFAULT, $1, 0, $2, $3, $4, NOW());
`
const minCaptionLen = 10
const maxCaptionLen = 200
caption := randString(minCaptionLen + rand.Intn(maxCaptionLen-minCaptionLen))
latitude := rand.Float32() * 90
longitude := rand.Float32() * 180
if _, err := tx.Exec(insertSQL, userID, caption, latitude, longitude); err != nil {
return err
}
const updateSQL = `
UPDATE users SET photoCount = photoCount + 1 WHERE id = $1;
`
if _, err := tx.Exec(updateSQL, userID); err != nil {
return err
}
return nil
}
示例8: Generate
func (packer *GrainPacker) Generate() []*api.Grain {
const groundOffset = 0.1
const noiseAmplitude = 0.001
offset := packer.org.Add(Vec3f{0, 0, groundOffset})
var grains []*api.Grain
for iz := 0; iz < packer.nz; iz++ {
for ix := 0; ix < packer.nx; ix++ {
for iy := 0; iy < packer.ny; iy++ {
for _, unitOffset := range packer.generateUnitOffsets() {
baseIndex := Vec3f{float32(ix), float32(iy), float32(iz)}
pos := baseIndex.Add(unitOffset).MultS(packer.latticeSize).Add(offset)
if packer.natural {
noiseBase := Vec3f{rand.Float32(), rand.Float32(), rand.Float32()}.MultS(2).SubS(1)
pos = pos.Add(noiseBase.MultS(noiseAmplitude))
}
grains = append(grains, &api.Grain{
Id: uint64(ix + 1),
Pos: &api.CkPosition{pos.X, pos.Y, pos.Z},
Vel: &api.CkVelocity{0, 0, 0},
Kind: packer.grainType,
CellProp: packer.MaybeGenerateCellProp(),
})
}
}
}
}
return grains
}
示例9: randomTracks
func randomTracks(num int) []*Track {
tracks := make([]*Track, num)
for i := 0; i < num; i++ {
var year int = rand.Int()%115 + 1900
var month int = rand.Int()%12 + 1
var day int = rand.Int()%29 + 1
var str_date = fmt.Sprintf("%d-%02d-%02d", year, month, day)
var date = parseTime(str_date)
tracks[i] = &Track{
date: date,
symbol: RandString(4),
open: rand.Float32(),
high: rand.Float32(),
low: rand.Float32(),
end: rand.Float32(),
}
}
sort.Sort(TrackByDate(tracks))
return tracks
}
示例10: Update
func (bs *BallSystem) Update(entity *ecs.Entity, dt float32) {
var space *engi.SpaceComponent
var speed *SpeedComponent
if !entity.Component(&space) || !entity.Component(&speed) {
return
}
if space.Position.X < 0 {
engi.Mailbox.Dispatch(ScoreMessage{1})
space.Position.X = 400 - 16
space.Position.Y = 400 - 16
speed.X = 800 * rand.Float32()
speed.Y = 800 * rand.Float32()
}
if space.Position.Y < 0 {
space.Position.Y = 0
speed.Y *= -1
}
if space.Position.X > (800 - 16) {
engi.Mailbox.Dispatch(ScoreMessage{2})
space.Position.X = 400 - 16
space.Position.Y = 400 - 16
speed.X = 800 * rand.Float32()
speed.Y = 800 * rand.Float32()
}
if space.Position.Y > (800 - 16) {
space.Position.Y = 800 - 16
speed.Y *= -1
}
}
示例11: TestReadComplex64
func TestReadComplex64(t *testing.T) {
var buf bytes.Buffer
wr := NewWriter(&buf)
rd := NewReader(&buf)
for i := 0; i < 100; i++ {
buf.Reset()
f := complex(rand.Float32()*math.MaxFloat32, rand.Float32()*math.MaxFloat32)
wr.WriteComplex64(f)
err := wr.Flush()
if err != nil {
t.Fatal(err)
}
out, err := rd.ReadComplex64()
if err != nil {
t.Error(err)
continue
}
if out != f {
t.Errorf("Wrote %f; read %f", f, out)
}
}
}
示例12: main
func main() {
data := [][]v3{}
world := world_new()
vdu := v3_divs(v3_sub(world.camera.rt, world.camera.lt), Width)
vdv := v3_divs(v3_sub(world.camera.lb, world.camera.lt), Height)
data = make([][]v3, Height)
for y := 0; y < Height; y++ {
data[y] = make([]v3, Width)
for x := 0; x < Width; x++ {
color := zero
ray := ray{}
ray.origin = world.camera.eye
for i := 0; i < Samples; i++ {
ray.direction = v3_unit(
v3_sub(
v3_add(
world.camera.lt,
v3_add(v3_muls(vdu, float32(x)+rand.Float32()),
v3_muls(vdv, float32(y)+rand.Float32()))),
world.camera.eye))
color = v3_add(color, trace(world, ray, 0))
}
color = v3_divs(color, Samples)
data[y][x] = color
}
}
writeppm(data)
}
示例13: OnDie
func (ms *Missle) OnDie(byTimer bool) {
if ms.Explosion == nil {
ms.GameObject().Destroy()
return
}
if ms.GameObject() == nil {
return
}
if !byTimer {
for i := 0; i < 2; i++ {
n := ms.Explosion.Clone()
n.Transform().SetParent2(GameSceneGeneral.Layer1)
n.Transform().SetWorldPosition(ms.Transform().WorldPosition())
s := n.Transform().Scale()
n.Transform().SetScale(s.Mul2(rand.Float32() + 0.5))
n.AddComponent(engine.NewPhysics(false))
n.Transform().SetRotationf(rand.Float32() * 360)
rot := n.Transform().Direction()
n.Physics.Body.SetVelocity(-rot.X*10, -rot.Y*10)
n.Physics.Body.SetMass(1)
n.Physics.Shape.Group = 1
n.Physics.Shape.IsSensor = true
}
}
ms.GameObject().Destroy()
}
示例14: TestLargeEuclideanIndex
func (suite *AnnoyTestSuite) TestLargeEuclideanIndex() {
index := annoyindex.NewAnnoyIndexEuclidean(10)
for j := 0; j < 10000; j += 2 {
p := make([]float32, 0, 10)
for i := 0; i < 10; i++ {
p = append(p, rand.Float32())
}
x := make([]float32, 0, 10)
for i := 0; i < 10; i++ {
x = append(x, 1+p[i]+rand.Float32()*1e-2)
}
y := make([]float32, 0, 10)
for i := 0; i < 10; i++ {
y = append(y, 1+p[i]+rand.Float32()*1e-2)
}
index.AddItem(j, x)
index.AddItem(j+1, y)
}
index.Build(10)
for j := 0; j < 10000; j += 2 {
var result []int
index.GetNnsByItem(j, 2, -1, &result)
assert.Equal(suite.T(), result, []int{j, j + 1})
index.GetNnsByItem(j+1, 2, -1, &result)
assert.Equal(suite.T(), result, []int{j + 1, j})
}
annoyindex.DeleteAnnoyIndexEuclidean(index)
}
示例15: Update
func (b *BounceSystem) Update(dt float32) {
for _, e := range b.entities {
if e.SpaceComponent.Position.X < 0 {
engo.Mailbox.Dispatch(ScoreMessage{1})
e.SpaceComponent.Position.X = 400 - 16
e.SpaceComponent.Position.Y = 400 - 16
e.SpeedComponent.X = 800 * rand.Float32()
e.SpeedComponent.Y = 800 * rand.Float32()
}
if e.SpaceComponent.Position.Y < 0 {
e.SpaceComponent.Position.Y = 0
e.SpeedComponent.Y *= -1
}
if e.SpaceComponent.Position.X > (800 - 16) {
engo.Mailbox.Dispatch(ScoreMessage{2})
e.SpaceComponent.Position.X = 400 - 16
e.SpaceComponent.Position.Y = 400 - 16
e.SpeedComponent.X = 800 * rand.Float32()
e.SpeedComponent.Y = 800 * rand.Float32()
}
if e.SpaceComponent.Position.Y > (800 - 16) {
e.SpaceComponent.Position.Y = 800 - 16
e.SpeedComponent.Y *= -1
}
}
}