
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Shuffle the Elements of an Array
An array is a fixed sequence of elements in which the elements are placed at contiguous memory locations. We will shuffle and randomly place the elements of the array. In the first case we will use fisher-yates shuffle algorithm. Let's see how we can execute it using different logics in the Golang code.
Syntax
rand.Seed(value)
Rand.Seed() function is used to generate random numbers. It takes a user input as argument which is the upper limit for generating random numbers.
func Now() Time
The Now() function is defined in time package. this function generates the current local time. to use this function we have to first impot the time package in our program.
func (t Time) UnixNano() int64
The UnixNano() function is defined in time package. this function is used to get the number of seconds passed from January 1 1970 in utc.The final result returned by it is of integer type of 64 bits.
rand.Intn(n)
The Intn() function is defined in the math/rand package. it is used to generate a random number in the interval from [0, n]. where n is a number provided by user. The function throws an error if the provided number is lesser than 0.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) time and math/rand packages
Step 2 ? In the main function create an array
Step 3 ? Use internal/ user-defined function to create random numbers
Step 4 ? Using user-defined function start the shuffling process.
Step 5 ? Print the output of the shuffled array
Example 1
In this example we will learn how to shuffle the elements of array using fisher yates shuffle algorithm.
package main import ( "fmt" "math/rand" //import fmt and math/rand ) //create main function to execute the program func main() { array := []int{10, 20, 30, 40, 50, 60, 70, 80} //create an array which is to shuffled fmt.Println("The elements of array are:", array) shuffle(array) //call the function shuffle fmt.Println("The shuffled array given here is:") fmt.Println(array) } // shuffle shuffles the elements of an array in place func shuffle(array []int) { for i := range array { //run the loop till the range of array j := rand.Intn(i + 1) //choose any random number array[i], array[j] = array[j], array[i] //swap the random element with current element } }
Output
The elements of array are: [10 20 30 40 50 60 70 80] The shuffled array given here is: [60 50 30 70 80 10 40 20]
Example 2
In this example we will use rand.Seed function to shuffle the elements of an array.
package main import ( "fmt" "math/rand" //import fmt, math/rand and time "time" ) //create main function to execute the program func main() { array := []int{10, 20, 30, 40, 50, 60} //create an array which is to shuffled fmt.Println("The elements of array are:", array) rand.Seed(time.Now().UnixNano()) // generate random numbers using this function shuffle(array) fmt.Println("The shuffled array is:") fmt.Println(array) } func shuffle(array []int) { for i := len(array) - 1; i > 0; i-- { //run the loop from the end till the start j := rand.Intn(i + 1) array[i], array[j] = array[j], array[i] //swap the random element with the current element } }
Output
The elements of array are: [10 20 30 40 50 60] The shuffled array is: [10 30 60 50 20 40]
Conclusion
We executed this program of shuffling an array using two set of examples. In the first example we used the fisher yates algorithm and in the second example we used rand.Seed function. We shuffled the array using different set of logic. Hence, program executed successfully.