Logic Building Episode 1 : Generate N digit random number based on input
Problem Statement: Write a function that generates an N digit random number based on the N input.
Input: N=5
Output: XXXXX (N digit random number)
Input: N= 2
Output: XX
Give try yourself, before look at the solution and share your solution in the comment.
package main
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
// random func will generate a random digit number based on param.
func random(param int) {
rand.Seed(time.Now().UnixNano())
chars := []rune("0123456789")
var b strings.Builder
for i := 1; i <= param; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
str := b.String()
i, err := strconv.Atoi(str)
if err != nil {
fmt.Println("error while convert")
}
fmt.Println(i)
}
// Driver
func main() {
random(5) // N = 5
}
Output:
λ random go run main.go
76929
λ random go run main.go
35246
λ random go run main.go
71719
λ random go run main.go
81434
λ random go run main.go
95233
λ random go run main.go
33170
λ random go run main.go
I'm really impressed with your writing skills, as smart as the structure of your weblog.
ReplyDeleteGoLand Full Crack
Waves v12 Complete Crack