generator random numbers in golang

package main

import "fmt"

func getRandomIntBetween0and9including(rand chan int) {
	for {
		select {
		case rand <- 0:
		case rand <- 1:
		case rand <- 2:
		case rand <- 4:
		case rand <- 5:
		case rand <- 5:
		case rand <- 6:
		case rand <- 7:
		case rand <- 8:
		case rand <- 9:
		}
	}
}

func main() {

	rand := make(chan int)
	go getRandomIntBetween0and9including(rand)

	fmt.Println(<-rand)
}

If You like number 2 and You want to get number 2 more often You can easy add line case rand <- 2: to select block and that’s it.

Publikováno v GoTagged