DEV Community

Abanoub Hanna
Abanoub Hanna

Posted on

A Code For Fun

I was bored. I couldn't code anything for my work or even revise some past work. I felt too lazy to do anything. So I sit down and kept thinking.. until an idea hit my mind.

I thought how many times does the auto-generated random number need to match another auto-generated random number. SO I thought of coding this question in code. As you know I love Go so I choose it to write my little code.

In code, I generated a random number and assigned it into x. Then generated another random number and assigned it to y. If x != y then I re-generate y random number until it matches the x random number. I added a counter for the false guesses of the random number.

My thinking conclusion is I need somewhat fixed number of guesses to match two random generated numbers as you can see in the video on YouTube.

I published the code on my github here and this is the code itself.

package main

import "fmt"
import "math/rand"

func main() {
    x := rand.Intn(33)
    y := rand.Intn(33)
    numberOfFalseGuesses := 0

    for x != y {
        fmt.Printf("x = %v and y = %v\n", x, y)
        y = rand.Intn(100)
        numberOfFalseGuesses++
    }
    fmt.Printf("finally x = y = %v\n", x)
    fmt.Printf("number of false guesses are %v", numberOfFalseGuesses)
}
Enter fullscreen mode Exit fullscreen mode

See you next blog. Keep in touch.

Top comments (0)