Not understanding simple array in struct

xuanbao · · 834 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m playing around go and was trying to create a simple way to roll 3 dice and get the individual results or the total and was confused by the result.</p> <p>In the code below I thought I was producing a new Roll struct, which would include a new array of 4 random results. Everytime I run the program though the results are exactly the same no matter what I do as if it&#39;s using the same array over and over again.</p> <p>Sorry for the basic question but what am I doing wrong?</p> <p><a href="https://play.golang.org/p/ueu8eLMxab">https://play.golang.org/p/ueu8eLMxab</a></p> <pre><code>package main import ( &#34;fmt&#34; &#34;math/rand&#34; ) type Roll struct { result [4]int } // func NewRoll() *Roll { roll := new(Roll) for i := range roll.result { roll.result[i] = rand.Intn(3) } return roll} func (r *Roll) Result() [4]int { return r.result } func (r *Roll) Total() int { var t int for i := range r.result { t = t + r.result[i] } return t } func main() { roll := NewRoll() fmt.Println(roll.Result()) fmt.Println(roll.Total()) } </code></pre> <hr/>**评论:**<br/><br/>jy3: <pre><p>I think I read somewhere that there is no random in the go playground . This may be your problem.</p></pre>SteazGaming: <pre><p>Many of the playground&#39;s system details are just spoofed. You can read more about it in this blog post: &#34;<a href="https://blog.golang.org/playground">https://blog.golang.org/playground</a>&#34;</p> <blockquote> <p>&#34;Although all these things are supported today, the first version of the playground, launched in 2010, had none of them. The current time was fixed at 10 November 2009, time.Sleep had no effect, and most functions of the os and net packages were stubbed out to return an EINVALID error.</p> <p>A year ago we implemented fake time in the playground, so that programs that sleep would behave correctly. A more recent update to the playground introduced a fake network stack and a fake file system, making the playground&#39;s tool chain similar to a normal Go tool chain. These facilities are described in the following sections.&#34;</p> </blockquote></pre>garslo: <pre><p>You need to seed the math/rand module. From <a href="http://golang.org/pkg/math/rand/#pkg-overview">math/rand</a>: &#34;Use the Seed function to initialize the default Source if different behavior is required for each run.&#34;</p> <p>A simple way is to do</p> <pre><code>rand.Seed(time.Now().Unix()) </code></pre> <p>in your main. Something in the chain (either math/rand or time) is limited in the playground, but run it locally and it gives different results each time.</p></pre>ProvokedGaming: <pre><p>You need to have a seeded random number generator based on something such as UnixTime. The problem is, time.Now() in the go playground is fixed and not updating. You can see that it is set to (2009-11-10 23:00:00 +0000 UTC) by doing a fmt.Printf(&#34;%s\n&#34;, time.Now()) in the playground.</p></pre>gschier2: <pre><p>Check out the docs on <a href="https://golang.org/pkg/math/rand/" rel="nofollow">random number generation</a>. Specifically &#34;Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run&#34;</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

834 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传