Random number issue in golang - go

This is the code I'm working with:
package main
import "fmt"
import "math/rand"
func main() {
code := rand.Intn(900000)
fmt.Println(code)
}
It always returns 698081. I don't understand, what the is problem?
https://play.golang.org/p/XisNbqCZls
Edit:
I tried rand.Seed
package main
import "fmt"
import "time"
import "math/rand"
func main() {
rand.Seed(time.Now().UnixNano())
code := rand.Intn(900000)
fmt.Println(code)
}
There is no change. Now it always returns 452000
https://play.golang.org/p/E_Wfm5tOdH
https://play.golang.org/p/aVWIN1Eb84

A couple of reasons why you'll see the same result in the playground
Golang playground will cache the results
The time in the playground always starts at the same time to make the playground deterministic.
Last but not least, the rand package default seed is 1 which will make the result deterministic. If you place a rand.Seed(time.Now().UnixNano()) you'll receive different results at each execution. Note that this won't work on the playground for the second reason above.

Related

Why does VS Code display a working function as "undeclared name"?

I made a very simple Go program, showing the current time. The function gets loaded from another source file with the same package (main).
For some reason, VS Code thinks, the function has an undeclared name.
Here's the code:
main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
displayTime()
fmt.Scanf("h")
}
show_time.go:
package main
import (
"fmt"
"time"
)
func displayTime() {
fmt.Println(time.Now())
}
It's in the same directory, and everything compiles right. It's just that red mark that won't go and would disturb me. Also, the red number gets added by 1, everytime I call a extern function..

Golang: How to generate random float using only crypto/rand

I'm using gosec to check if my code has any security flaws. But it is reporting the use of math/rand (Use of weak random number generator (math/rand instead of crypto/rand)) pkg in this code:
package main
import (
"fmt"
"math/rand"
)
func main() {
a := rand.Float64()
fmt.Println(a)
}
The problem is: crypto/rand does not have the option to get a random float: https://pkg.go.dev/crypto/rand
How can I do that?

Change hex to string

I'm going through the Golang tutorial, and I am on this part
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Seed)
}
This returns My favorite number is 0xb1c20
I have been reading on https://golang.org/pkg/math/rand/#Seed but I'm still a bit confused as to how have it instead of show the hex show a string
math/rand.Seed is a function; you are printing the function's location in memory. You probably meant to do something like the following:
package main
import (
"fmt"
"math/rand"
)
func main() {
rand.Seed(234) // replace with your seed value, or set the seed based off
// of the current time
fmt.Println("My favorite number is", rand.Int())
}

Why TrimLeft doesn't work as expected?

I've expected tag to be "account" but it is "ccount". Why is "a" removed?
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimLeft(s, "refs/tags")
fmt.Println(tag)
}
Run
Use TrimPrefix instead of TrimLeft
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimPrefix(s, "refs/tags/")
fmt.Println(tag)
}
Please notice that following TrimLeft calls will result the same "fghijk
" string:
package main
import (
"fmt"
"strings"
)
func main() {
s := "/abcde/fghijk"
tag := strings.TrimLeft(s, "/abcde")
fmt.Println(tag)
tag = strings.TrimLeft(s, "/edcba")
fmt.Println(tag)
}
So TrimLeft is not the method which fits your needs. I guess it's impossible to use it in the example you've given to get the result you expect.
It is working as documented:
TrimLeft returns a slice of the string s with all leading Unicode
code points contained in cutset removed
Because there's an 'a' in the first argument (the cutset) the leading 'a' in account is removed

how to generate a random token with md5

I'm trying to generate a random token that I can use while implementing reset password functionality. This (http://play.golang.org/p/mmAzXLIZML) is the dazzling and non-functional :( code that I came up with for a first try. It doesn't work as I'd hope because it produces the same token over and over again (which I assume is a function of the time not changing). How do I generate a random token with md5 that will change every time?
package main
import "fmt"
import "strconv"
import "time"
import "crypto/md5"
import "io"
func main() {
time := strconv.FormatInt(time.Now().Unix(), 10)
fmt.Println(time)
h := md5.New()
io.WriteString(h, time)
fmt.Printf("%x", h.Sum(nil))
}
http://play.golang.org/p/mmAzXLIZML
It generates the same result each time only because it's in the playground, where time is frozen and pages are cached.
This isn't a great idea though, since a reset password could be guessed based on the time the request was made.
Why does it have to be an md5? Here's a random token generator:
http://play.golang.org/p/3weHBU6YZr
func randToken() string {
b := make([]byte, 8)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
uuid is annother chooice. see
go get "code.google.com/p/go-uuid/uuid"
and the func uuid.New() is what you wanted.

Resources