Random number with timestamp

HI Everyone,

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.

package main

import “fmt”
import “math/rand”

func main() {

k1 := rand.Intn(30)
fmt.Println(k1)

}

8 decimal, hexadecimal, octal or binary digits?

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.

If you want something truly unique, just use a uuid.

But otherwise @jayts approach should be sufficient, it will provide reasonable entropy on real life machines.

Feel free to use my token package :sunglasses:

Hi Nobbz, Yes I want very unique. Because I want to use that as key for some other purpose. Do you have any examples of uuid please?

Not at hand and not in golang. But as they are very common I’m sure there are packages available.

Is a popular uuid package for go.

Be careful, that repo was flagged for inactivity and known bugs not getting attended (see https://github.com/satori/go.uuid/issues/84). The community forked it and is being maintained at https://github.com/gofrs/uuid.

Oddly enough, there’s a new commit from a few days ago at the original repo, but there’s no activity since april before that.

1 Like

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 }

can you please elaborate bit more on this.

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 >=.

Thanks a lot @jayts it makes lot more sense.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.