SoFunction
Updated on 2025-03-05

Interpretation of the role and explanation of UnixNano

(().UnixNano()) function

Get random numbers without adding random seeds. Each traversal is used to obtain some random data that is repeated.

(().UnixNano())

Setting a random number seed and adding this line of code can ensure that every random number is random

golang rand seed random number

(().UnixNano())
randInsert := (4) // [0, 4)

If (x) is adjusted before each adjustment, and the x is the same every time, the () is the same every time.

Two solutions

1. Only call() once: call seed once in the global initialization, and do not call() before each call.

2. Adjust (x), but each time x is guaranteed to be different: use nanosecond-level seeds each time. This is highly not recommended, as nanoseconds may also be repeated in case of high concurrency.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.