I want to generate the random number based on timestamp. Can anyone please help in this regards. I have been trying to with math/rand package but not getting expected output.
i want 8 digit random number to be generated based on timestamp. Can anyone help please, if any example for reference would help me a lot.
Created from timestamp in what way? If the randomizer is fed with the same timestamp again, shall it give you the same number as before? How is this different from using a randomizer at all? Or do you actually need the time as a seed and want to create the same sequence of random values given a timestamp?
I don’t understand the question either, but maybe this is what you want, or it is similar.
The function srandom() initializes the random number generator based on the current system clock. From there, you can call random() to get a random int. (It is meant to work like srandom() and random() from C’s standard library.)
This program will give random integers that are exactly 8 decimal digits, not fewer.
Be careful and test it. In some virtualized environments (for example, the Go Playground), the random number generator is seeded the same every time, so it always gives the same sequence of random numbers.
package main
import (
"fmt"
"math/rand"
"time"
)
var randomgen *rand.Rand
func srandom() {
randomgen = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func random() int {
return randomgen.Int()
}
func main() {
var rn int
srandom() // initialize random number generator
for i := 0; i < 20; i++ {
for {
rn = random() % 100000000
if rn > 10000000 { break }
}
fmt.Printf("%d\n", rn)
}
}
Hi Jayts, sorry if my questions was not clear.
Actually this is what I am trying to implement- every time my program called I want to generate one random number which should be unique, I see the only way I could do it for me is a with the help of time stamp multiply by rand.intn.But I was unable to implement successfully. I tried with rant.Intn(30) * time.
Can you please help me on this. Please do let me know if still my question is not clear.
I’m not sure if I understand you. You can use the code I showed you, and just generate one random number instead of 20. It will give you a different random number every time the program is run, as long as time.Now() is working reasonably. (Just don’t expect good behavior in the Go Playground or other simulated environment.)
Is there any reason why the following will not work for you?
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var rn int
for {
rn = rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 100000000
if rn > 10000000 { break }
}
fmt.Printf("%d\n", rn)
}
Thanks a lot jayts. This is exactly what i was looking for.
Just one doubt please. may i understand please why you have used below statement
if rn > 10000000 { break }
You said you wanted an 8-digit number. The statement checks to see if the number is larger than 10000000 to make sure it has 8 digits, and if it is too small, it will try again. Actually, I should have used >=.