-1

I want to get multiple random elements from list in same time, but need to be real random not every time same elements.

Second problem is : i want to get uniq element from the list, example if i get 08, this code need to be removed from list and get net random exclude 08 string.

this is my actual code :

    package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    // Won't work on the Playground since the time is frozen.
    rand.Seed(time.Now().Unix())
    reasons := []string{
        "01","02","03","04", "05", "06", "07", "08", "09", "10",
    }
    n := rand.Int() % len(reasons)
    n1 := rand.Int() % len(reasons)
    n2 := rand.Int() % len(reasons)
    fmt.Println("", reasons[n])
    fmt.Println("", reasons[n1])
    fmt.Println("", reasons[n2])
}

my output is this :

 07
 02
 06

every time return me same output, i want to be random and unique

JimB
  • 104,193
  • 13
  • 262
  • 255
Stefano Conte
  • 85
  • 2
  • 6
  • 1
    It is different every time, unless `time.Now()` is the same every time you run it. – JimB May 09 '18 at 17:41
  • https://stackoverflow.com/a/33994791/594589 – dm03514 May 09 '18 at 17:42
  • Note that running this multiple times within a second will produce the same results, since `time.Now().Unix()` is measured in whole seconds. If you want a more fine grained timestamp, use `UnixNano()` – JimB May 09 '18 at 17:52
  • @JimB in my computer return 09 09 09, ---- 01 01 01 – Stefano Conte May 09 '18 at 17:54
  • @JimB with .UnixNano() i have same problem, return same number for all 3 random try – Stefano Conte May 09 '18 at 17:55
  • 1
    @StefanoConte: that's different than what you state in the question, where it's 3 different indexes, but the same result on subsequent runs. Which is it? 09, 09, 09 is unlikely, but still possible. Is that the exact code you're executing? – JimB May 09 '18 at 17:59
  • If you want 3 random *distinct* elements, you should look at [rand.Shuffle](https://golang.org/pkg/math/rand/#Shuffle) and/or [rand.Perm](https://golang.org/pkg/math/rand/#Perm). – Adrian May 09 '18 at 19:33

1 Answers1

0

To get a unique answer all you have to do is remove the existing answer from the slice. I have knocked something together which does that using a function. There are more efficient ways to do it, but this is simple to see what is happening.

func main() {
    rand.Seed(time.Now().Unix())
    reasons := []string{
        "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
    }

    n, reasons := getRandom(reasons)
    n1, reasons := getRandom(reasons)
    n2, reasons := getRandom(reasons)
    fmt.Println("", n)
    fmt.Println("", n1)
    fmt.Println("", n2)
}

func getRandom(reasons []string) (string, []string) {
    n := rand.Int() % len(reasons)
    switch n {
    case 0:
        return reasons[0], reasons[1:]
    case len(reasons) - 1:
        return reasons[n], reasons[:n]
    }
    newSlice := []string{}
    for i, item := range reasons {
        if i != n {
            newSlice = append(newSlice, item)
        }
    }
    return reasons[n], newSlice
}
dgm
  • 113
  • 1
  • 3