secureRandomFloat64 generates a float64 value in the range [0, 1) using crypto/rand. Optimized version with better error handling and performance.
()
| 200 | // secureRandomFloat64 generates a float64 value in the range [0, 1) using crypto/rand. |
| 201 | // Optimized version with better error handling and performance. |
| 202 | func secureRandomFloat64() float64 { |
| 203 | var b [8]byte |
| 204 | if _, err := rand.Read(b[:]); err != nil { |
| 205 | // Use a fallback random value instead of 0 to maintain jitter |
| 206 | return 0.5 // Middle value for consistent jitter behavior |
| 207 | } |
| 208 | // Use bit shifting instead of division for better performance |
| 209 | return float64(binary.BigEndian.Uint64(b[:])) / (1 << 63) / 2.0 |
| 210 | } |
| 211 | |
| 212 | // EnsureDBVersion checks the version of the given database connection |
| 213 | // and returns an error if the version is not supported. |