This tutorial lets you know, How to generate a random number using Golang slices. I will use a time package to generate a random number and store them into slices using the UNIX timestamp.

We’ll disscuss many scenarios to generate random numbers.You can use generate random number in you golang application based on needs.

The golang provides **math/rand** package to genrate random number.

Table of Contents

## Common Scenario to Generate Random Numbers

**Seeding Number:**Sometimes, We need to seed some numbers against a column for a database table or sample data.**Cryptographically Secure Randomness:**Use the`crypto/rand`

package to generate secure random numbers if your application needs cryptographic security.**Range Number:**We can also generate random numbers within a specific range.

## Random Number Slicing

Let’s explore different scenarios with example to generate random numbers in golang application. Random numbers play a crucial role in gaming and to cryptographic algorithms.

### How to Generate Random Number Slice Using Unix Timestamp

Let’s creates methods that ll generate a slice of 20 number, Which have some values that are negative and some that are positive.

( "fmt" "math/rand" "time" ) func generateRandomSlice(size int) []int { slice := make([]int, size, size) rand.Seed(time.Now().UnixNano()) for i := 0; i < size; i++ { slice[i] = rand.Intn(999) - rand.Intn(999) } return slice } slice := generateRandomSlice(20) fmt.Println("\n--- The Random Slices of 20 Number ---\n\n", slice, "\n")

I have imported `math/rand`

,time package to generate UNIX timestamp. Both methods are identically the same but different with respect to values, the first method will return both type values, negative and positive integer but the second method will return only positive numbers.

### How to Generate Positive Random Number

The below method will generate a slice of 10 number, Which have only a positive number.

( "fmt" "math/rand" "time" ) func generateRandomNumber(size int) []int { rand_number := make([]int, size, size) for i := 0; i < size; i++ { rand_number[i] = rand.Intn(100) } return rand_number } rand_number := generateRandomNumber(10) fmt.Println("\n--- The Random Number of 10 Length ---\n\n", rand_number, "\n")

### Floating-Point Random Numbers

We can also generate a slice of random floating-point numbers:

package main import ( "fmt" "math/rand" "time" ) func generateRandomFloatSlice(length int, min, max float64) []float64 { rand.Seed(time.Now().UnixNano()) randomFloatSlice := make([]float64, length) for i := 0; i < length; i++ { randomFloatSlice[i] = rand.Float64()*(max-min) + min } return randomFloatSlice } func main() { randomFloatSlice := generateRandomFloatSlice(5, 1.0, 5.0) fmt.Println("Random Float Slice:", randomFloatSlice) }

### Random Password Generation

A common technique for creating secure passwords is to use randomness. Let’s write a programme to produce a password at random:

package main import ( "fmt" "math/rand" "time" ) const ( lowercaseLetters = "abcdefghijklmnopqrstuvwxyz" uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" digits = "0123456789" specialChars = "!@#$%^&*()-_=+[]{}|;:'\",.<>/?" ) func generatePassword(length int) string { rand.Seed(time.Now().UnixNano()) allChars := lowercaseLetters + uppercaseLetters + digits + specialChars password := make([]byte, length) for i := 0; i < length; i++ { password[i] = allChars[rand.Intn(len(allChars))] } return string(password) } func main() { randomPassword := generatePassword(12) fmt.Println("Random Password:", randomPassword) }

In the above code, I have created a `generatePassword()`

function that is used to create randomly password by using a pool of lowercase, uppercase, numbers, and special characters.