pick Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/pick/ Tue, 31 Mar 2020 15:59:28 +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 pick Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/pick/ 32 32 159787465 Pick a random character in string in Go (Golang) https://vikasboss.github.io/pick-random-character-string-golang/ https://vikasboss.github.io/pick-random-character-string-golang/#respond Tue, 31 Mar 2020 15:59:17 +0000 https://vikasboss.github.io/?p=1918 Overview ‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can...

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

]]>
Overview

‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can be utilized to pick a random element in a string. We can generate a random between 0 and length-1 of string. Then we can use that random number to index into the string and get the result.

But there is one problem in above approach. 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 -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 below program for picking a random in a given string , we  are first converting a string into a rune array so that we can index into the rune array and then return the random character.

Code

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    in := "abcdedf£"
    inRune := []rune(in)
    randomIndex := rand.Intn(len(inRune))
    pick := inRune[randomIndex]
    fmt.Println(string(pick))
}

Output:

One of a,b,c,d,e,f,£

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

]]>
https://vikasboss.github.io/pick-random-character-string-golang/feed/ 0 1918
Pick a random element in an array or slice in Go (Golang) https://vikasboss.github.io/pick-random-element-array-slice-go/ https://vikasboss.github.io/pick-random-element-array-slice-go/#respond Tue, 31 Mar 2020 15:55:50 +0000 https://vikasboss.github.io/?p=1912 Overview ‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can...

The post Pick a random element in an array or slice in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can be utilized to pick a random element in an array or slice of int or string.

To know more about what pseudo-random number means, checkout this post – https://vikasboss.github.io/generate-random-number-golang

Below is the signature of this method. It takes input a number n and will return a number x in range 0<=x<n.

func Intn(n int) int

Code

We can directly index an element in a slice of int. See below program for picking up random from a slice of int.

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    in := []int{2, 5, 6}
    randomIndex := rand.Intn(len(in))
    pick := in[randomIndex]
    fmt.Println(pick)
}

Output:

Between 2, 5 or 6

The post Pick a random element in an array or slice in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/pick-random-element-array-slice-go/feed/ 0 1912