Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,651 questions

51,528 answers

573 users

How to use the clock as a random generator seed in Go

2 Answers

0 votes
package main
 
import (
    "fmt"
    "math/rand"
    "time"
)
 
func main() {
    // Seed the random number generator using the current time
    rand.Seed(time.Now().UnixNano())
 
    // Generate a random number
    randomNumber := rand.Intn(100) // Random number between 0 and 99
    fmt.Println("Random number:", randomNumber)
    
    randomNumber = rand.Intn(100) // Random number between 0 and 99
    fmt.Println("Random number:", randomNumber)
}
 
 
/*
run:
 
Random number: 37
Random number: 51
 
*/

 



answered May 8, 2025 by avibootz
edited May 8, 2025 by avibootz
0 votes
package main

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

func main() {
    rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

    fmt.Println("Random number:", rnd.Intn(1000))
    fmt.Println("Random number:", rnd.Intn(1000))
}


/*
run:

Random number: 224
Random number: 551

*/

 



answered May 8, 2025 by avibootz
...