本文整理汇总了Golang中math/rand.Rand.Float32方法的典型用法代码示例。如果您正苦于以下问题:Golang Rand.Float32方法的具体用法?Golang Rand.Float32怎么用?Golang Rand.Float32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/rand.Rand
的用法示例。
在下文中一共展示了Rand.Float32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NextCall
func (ss *SessionScenario) NextCall(rg *rand.Rand) (*Call, error) {
for {
if i := rg.Intn(ss.SessionAmount); i >= 0 {
select {
case st := <-ss._sessions[i].StepLock:
switch st {
case STEP1:
if ss._sessions[i]._calls[st].GenParam != nil {
ss._sessions[i]._calls[st].Method, ss._sessions[i]._calls[st].Type, ss._sessions[i]._calls[st].URL, ss._sessions[i]._calls[st].Body = ss._sessions[i]._calls[st].GenParam()
}
// execute session call for the first time
return ss._sessions[i]._calls[st], nil
default:
// choose a non-initialized call randomly
ss._sessions[i].StepLock <- REST
q := rg.Float32() * ss._sessions[i]._totalWeight
for j := STEP1 + 1; j < ss._sessions[i]._count; j++ {
if q <= ss._sessions[i]._calls[j].RandomWeight {
if ss._sessions[i]._calls[j].GenParam != nil {
ss._sessions[i]._calls[j].Method, ss._sessions[i]._calls[j].Type, ss._sessions[i]._calls[j].URL, ss._sessions[i]._calls[j].Body = ss._sessions[i]._calls[j].GenParam()
}
return ss._sessions[i]._calls[j], nil
}
}
}
default:
continue
}
}
}
log.Fatal("what? should never reach here")
return nil, errors.New("all sessions are being initialized")
}
示例2: doOneStep
func doOneStep(sys *StressLattice, alpha, stressNoise float32, R uint8, Ran *rand.Rand) uint {
maxLoc := sys.FindMaxAllStress()
currStress := sys.GetStress(maxLoc)
sys.LoadSystem(sys.failureStress - currStress)
currStress = sys.failureStress
newStress := (stressNoise * (Ran.Float32()*2.0 - 1.0)) + sys.residualStress
stressDistOut := (1.0 - alpha) * (currStress - newStress)
//distribute stress
sys.RangedDistStress(maxLoc, stressDistOut, newStress, R, sys.dim)
eventSize := uint(1)
// distribute until no more failed sites exist
for sys.DistributeFailedSites() {
maxLoc = sys.FindLargestDistributeFail()
// stress change
currStress = sys.GetStress(maxLoc)
newStress = (stressNoise * (Ran.Float32()*2.0 - 1.0)) + sys.residualStress
// stress to be distributed
stressDistOut = (1.0 - alpha) * (currStress - newStress)
sys.RangedDistStress(maxLoc, stressDistOut, newStress, R, sys.dim)
eventSize = eventSize + 1
}
return eventSize
}
示例3: MonteCarloPixel
func MonteCarloPixel(results chan Result, scene *geometry.Scene, diffuseMap /*, causticsMap*/ *kd.KDNode, start, rows int, rand *rand.Rand) {
samples := Config.NumRays
var px, py, dy, dx geometry.Float
var direction, contribution geometry.Vec3
for y := start; y < start+rows; y++ {
py = scene.Height - scene.Height*2*geometry.Float(y)/geometry.Float(scene.Rows)
for x := 0; x < scene.Cols; x++ {
px = -scene.Width + scene.Width*2*geometry.Float(x)/geometry.Float(scene.Cols)
var colourSamples geometry.Vec3
if x >= Config.Skip.Left && x < scene.Cols-Config.Skip.Right &&
y >= Config.Skip.Top && y < scene.Rows-Config.Skip.Bottom {
for sample := 0; sample < samples; sample++ {
dy, dx = geometry.Float(rand.Float32())*scene.PixH, geometry.Float(rand.Float32())*scene.PixW
direction = geometry.Vec3{
px + dx - scene.Camera.Origin.X,
py + dy - scene.Camera.Origin.Y,
-scene.Camera.Origin.Z,
}.Normalize()
contribution = Radiance(geometry.Ray{scene.Camera.Origin, direction}, scene, diffuseMap /*causticsMap,*/, 0, 1.0, rand)
colourSamples.AddInPlace(contribution)
}
}
results <- Result{x, y, colourSamples.Mult(1.0 / geometry.Float(samples))}
}
}
}
示例4: RandDuration
func (d *DelayAction) RandDuration(r *rand.Rand) time.Duration {
t := d.Time
if d.Rand {
t *= r.Float32()
}
return (time.Duration)(t * 1000000000)
}
示例5: buildResponse
func (r *RequestsHandler) buildResponse(rd *rand.Rand, req *openrtb.Request) openrtb.Response {
cur := "RUB"
pr := rd.Float32()
return openrtb.Response{
Id: req.Id,
Seatbid: []openrtb.Seatbid{openrtb.Seatbid{Bid: []openrtb.Bid{openrtb.Bid{Id: req.Imp[0].Id, Adm: &r.Markup, Price: &pr, Impid: req.Imp[0].Id}}}},
Cur: &cur,
}
}
示例6: RandomRatPercent
func RandomRatPercent(randgen *rand.Rand) uint {
if randgen == nil {
return 10
}
if randgen.Float32() < 0.40 {
return uint(randgen.Intn(21) + 10)
}
return 0
}
示例7: gaussianRandom
// From -1 to 1
func gaussianRandom(r *rand.Rand) float32 {
// Add n numbers from r, this will give us a gaussian distribution
// then we just need to set the variance and mean correctly
const n = 25
value := float32(0.0)
for i := 0; i < n; i++ {
value += r.Float32()
}
value -= n / 2.0
value *= 2.0 / n
return value
}
示例8: NextCall
func (s *Scenario) NextCall(rg *rand.Rand) (*Call, error) {
r := rg.Float32() * s._totalWeight
for i := 0; i < s._count; i++ {
if r <= s._calls[i].RandomWeight {
if s._calls[i].GenParam != nil {
s._calls[i].Method, s._calls[i].Type, s._calls[i].URL, s._calls[i].Body = s._calls[i].GenParam()
}
return s._calls[i], nil
}
}
return nil, errors.New("something wrong with randomize number")
}
示例9: randNextGps
func randNextGps(last *GPSData, rander *rand.Rand) *GPSData {
last.Time += 500 + int64(rander.Float32()*1000)
last.Latitude += (rander.Float64()*2 - 1) * 0.0001
last.Longitude += (rander.Float64()*2 - 1) * 0.0001
if last.Speed < 1 {
last.Speed += (rander.Float64()*2 - 0.5) * 1
} else if last.Speed > 8 {
last.Speed += (rander.Float64()*2 - 1.5) * 1
} else {
last.Speed += (rander.Float64()*2 - 1) * 2
}
if last.Speed <= 0 {
last.Speed = rander.Float64() * 2
}
fmt.Println("{\"lng\":" + strconv.FormatFloat(last.Longitude, 'f', 6, 64) +
",\"lat\":" + strconv.FormatFloat(last.Latitude, 'f', 6, 64) + ",\"count\":1},")
return last
}
示例10: Generate
func (b bytesize) Generate(rand *rand.Rand, size int) reflect.Value {
suffix := ""
number := 1 + rand.Int31n(int32(size))
chance := rand.Float32()
if chance < 0.1 {
suffix = "k"
}
// Can't test `kb`-style suffixes in darwin…
return reflect.ValueOf(bytesize(fmt.Sprintf("%d%s", number, suffix)))
}
示例11: NextCall
func (ss *HCScenario) NextCall(rg *rand.Rand) (*Call, error) {
for {
if i := rg.Intn(ss.SessionAmount); i >= 0 {
select {
case st := <-ss._sessions[i].StepLock:
switch st {
case STEP1, STEP2, STEP3:
// execute session call for the first time
if ss._sessions[i]._calls[st].GenParam != nil {
ss._sessions[i]._calls[st].Method, ss._sessions[i]._calls[st].Type, ss._sessions[i]._calls[st].URL, ss._sessions[i]._calls[st].Body = ss._sessions[i]._calls[st].GenParam(ss._sessions[i].Storage["seq"], ss._sessions[i].Storage["player_id"])
}
return ss._sessions[i]._calls[st], nil
default:
// choose a non-initialized call randomly
ss._sessions[i].StepLock <- REST
q := rg.Float32() * ss._sessions[i]._totalWeight
//for j := STEP2 + 1; j < ss._sessions[i]._count; j++ {
for j := STEP3 + 1; j < ss._sessions[i]._count; j++ {
if q <= ss._sessions[i]._calls[j].RandomWeight {
// add 1 to seq
ss._sessions[i].InternalLock.Lock()
seq, _ := strconv.ParseInt(ss._sessions[i].Storage["seq"], 10, 64)
atomic.AddInt64(&seq, 1)
ss._sessions[i].Storage["seq"] = strconv.FormatInt(seq, 10)
if ss._sessions[i]._calls[j].GenParam != nil {
ss._sessions[i]._calls[j].Method, ss._sessions[i]._calls[j].Type, ss._sessions[i]._calls[j].URL, ss._sessions[i]._calls[j].Body = ss._sessions[i]._calls[j].GenParam(ss._sessions[i].Storage["seq"], ss._sessions[i].Storage["player_id"])
}
ss._sessions[i].InternalLock.Unlock()
return ss._sessions[i]._calls[j], nil
}
}
}
default:
continue
}
}
}
log.Fatal("what? should never reach here")
return nil, errors.New("all sessions are being initialized")
}
示例12: makeRoutes
func makeRoutes(ps []place, r *rand.Rand) {
for i := range ps {
p := &ps[i]
others := r.Perm(len(ps))
percentageToConnectTo := r.Float32()
next := i + 1
if next == len(ps) {
next = 0
}
p.neighbours = append(p.neighbours, dist{to: int32(next), cost: calcDist(p, &ps[next])})
for _, o := range others {
var newNeighbour dist
if r.Float32() < percentageToConnectTo && o != next && o != i {
newNeighbour = dist{to: int32(o), cost: calcDist(p, &ps[o])}
} else {
newNeighbour = dist{-1, -1}
}
p.neighbours = append(p.neighbours, newNeighbour)
}
}
}
示例13: SamplePoint
func (self *Triangle) SamplePoint(r *rand.Rand) Vector3f {
/* get two randoms */
sqr1 := math32.Sqrt(r.Float32())
r2 := r.Float32()
/* make barycentric coords */
c0 := 1.0 - sqr1
c1 := (1.0 - r2) * sqr1
/* make barycentric axes */
a0 := self.Vertexs[1].Sub(self.Vertexs[0])
a1 := self.Vertexs[2].Sub(self.Vertexs[0])
/* scale axes by coords */
ac0 := a0.Mulf(c0)
ac1 := a1.Mulf(c1)
/* sum scaled components, and offset from corner */
sum := ac0.Add(ac1)
return sum.Add(self.Vertexs[0])
}
示例14: randNextHR
func randNextHR(last *HeartRateData, rander *rand.Rand) *HeartRateData {
last.Time += 500 + int64(rander.Float32()*1000)
if last.HeartRate < 80 {
last.HeartRate += int((rander.Float32()*2 - 0.5) * 10)
} else if last.HeartRate < 160 {
last.HeartRate += int((rander.Float32()*2 - 1.5) * 10)
} else {
last.HeartRate += int((rander.Float32()*2 - 1) * 10)
}
if last.HeartRate < 80 {
last.HeartRate = 86
}
return last
}
示例15: makeRandomModel
// Make a network of nodes with random topology
func makeRandomModel(params *TestParams, r *rand.Rand, t *testing.T) *Model {
m := Model{
t: t,
r: r,
quorum: params.nodeCount/2 + 1,
nodes: make([]TestNode, params.nodeCount),
readyLinks: []*Link{},
nextID: params.nodeCount + 1,
}
for i := range m.nodes {
m.nodes[i].Node = NewNode(router.PeerName(i/2+1),
router.PeerUID(r.Int63()), m.quorum)
m.nodes[i].Propose()
}
for i := 1; i < len(m.nodes); i++ {
// was node i connected to the other nodes yet?
connected := false
for j := 0; j < i; j++ {
if r.Float32() < params.connectedProb {
connected = true
m.addLink(&m.nodes[i], &m.nodes[j])
}
}
if !connected {
// node i must be connected into the graph
// somewhere. So if we didn't connect it
// already, this is a last resort.
m.addLink(&m.nodes[i], &m.nodes[r.Intn(i)])
}
}
return &m
}