shuffle Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/shuffle/ Mon, 13 Apr 2020 18:51:37 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 shuffle Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/shuffle/ 32 32 159787465 Shuffle a slice or array in Go (Golang) https://vikasboss.github.io/shuffle-slice-or-array-go/ https://vikasboss.github.io/shuffle-slice-or-array-go/#respond Thu, 02 Apr 2020 06:53:18 +0000 https://vikasboss.github.io/?p=1928 Overview math/rand package of go provides a Shuffle method that can be used shuffle an array or a slice. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means...

The post Shuffle a slice or array in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math/rand package of go provides a Shuffle method that can be used shuffle an array or a slice. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for a fixed input seed it will generate the same randomization. That is why in our program we will initialize the rand package with a different seed every time.

Below is the signature of the function.

func Shuffle(n int, swap func(i, j int))

This function takes in arguments

  • First is the length of the array or slice.
  • The second is a swap function that will be called for different indexes i and j. You need to provide your own swap function that will swap your elements in the array.

Also note that this function will panic if n<0. Let’s look at the code.

Code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())

    in := []int{2, 3, 5, 8}
    rand.Shuffle(len(in), func(i, j int) {
        in[i], in[j] = in[j], in[i]
    })
    fmt.Println(in)

    rand.Shuffle(len(in), func(i, j int) {
        in[i], in[j] = in[j], in[i]
    })
    fmt.Println(in)
}

Output:

It can produce a different output on your machine.

[5 3 2 8]
[3 5 8 2]

The post Shuffle a slice or array in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/shuffle-slice-or-array-go/feed/ 0 1928
Shuffle a string in Go (Golang) https://vikasboss.github.io/shuffle-string-golang/ https://vikasboss.github.io/shuffle-string-golang/#respond Thu, 02 Apr 2020 06:45:12 +0000 https://vikasboss.github.io/?p=1921 Overview math/rand package of GO provides a Shuffle method that can be used to shuffle a string. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for...

The post Shuffle a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math/rand package of GO provides a Shuffle method that can be used to shuffle a string. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for a fixed input seed it will generate the same randomization. That is why in our program we will initialize the rand package with a different seed every time.

Below is the signature of the function.

func Shuffle(n int, swap func(i, j int))

This function takes in below arguments

  • First is the total number of characters in the given string.
  • The second is a swap function that will be called for different indexes i and j. You need to provide your own swap function that will swap your elements in the string.

Also note that this function will panic if n<0. In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 to 4 bytes. Due to this it is not possible to index a character in a string.  In GO, rune data type represents a Unicode point.  Once a string is converted to an array of rune then it is possible to index a character in that array of rune.

You can learn more the above issue here – https://vikasboss.github.io/number-characters-string-golang/

For this reason in the below program for shuffling a given string , we are first converting a string into a rune array so that we can index into the rune array and use that index to swap characters in that string to shuffle the string.

Code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())

    in := "abcdedf£"

    inRune := []rune(in)
    rand.Shuffle(len(inRune), func(i, j int) {
        inRune[i], inRune[j] = inRune[j], inRune[i]
    })
    fmt.Println(string(inRune))

    rand.Shuffle(len(inRune), func(i, j int) {
        inRune[i], inRune[j] = inRune[j], inRune[i]
    })
    fmt.Println(string(inRune))
}

Output:

It will produce a different output on your machine.

dd£cebaf
feb£cadd

The post Shuffle a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/shuffle-string-golang/feed/ 0 1921