Skip to content

Instantly share code, notes, and snippets.

@Oppodelldog
Created November 25, 2015 22:16
Show Gist options
  • Save Oppodelldog/f2487cb2ccc6fdd0d561 to your computer and use it in GitHub Desktop.
Save Oppodelldog/f2487cb2ccc6fdd0d561 to your computer and use it in GitHub Desktop.
short test of randomgenerated, structured data,trying to create scope safe random numbers
package main
import (
"fmt"
"math/rand"
)
type OfferGenerator struct {
offers []Offer
}
type RoomGenerator struct {
rooms []Room
}
type Offer struct {
name string
rooms []Room
}
type Room struct {
prices []Price
}
type Price struct {
price int
}
var offerNameRand = rand.New(rand.NewSource(0))
var priceRand = rand.New(rand.NewSource(0))
var olRand = rand.New(rand.NewSource(0))
var rlRand = rand.New(rand.NewSource(int64(10)))
func generateOffers() []Offer {
rand.Seed(0)
offers := []Offer{}
offerCount := olRand.Intn(5)
for oIdx := 0; oIdx < offerCount; oIdx++ {
offer := Offer{}
offer.name = []string{"a", "b", "c", "d", "e", "f"}[offerNameRand.Intn(6)]
roomCount := rlRand.Intn(5)
for rIdx := 0; rIdx < roomCount; rIdx++ {
room := Room{}
var plRand = rand.New(rand.NewSource(int64(oIdx)))
priceRand = rand.New(rand.NewSource(int64(rIdx)))
priceCount := plRand.Intn(5)
for pIdx := 0; pIdx < priceCount; pIdx++ {
price := Price{}
price.price = priceRand.Intn(9)
room.prices = append(room.prices, price)
}
offer.rooms = append(offer.rooms, room)
}
offers = append(offers, offer)
}
return offers
}
func main() {
offers := generateOffers()
for oIdx := 0; oIdx < len(offers); oIdx++ {
offer := offers[oIdx]
fmt.Printf("%v - ", offer.name)
fmt.Print("[")
for rIdx := 0; rIdx < len(offer.rooms); rIdx++ {
room := offer.rooms[rIdx]
fmt.Printf(" ")
for pIdx := 0; pIdx < len(room.prices); pIdx++ {
price := room.prices[pIdx]
fmt.Printf("%v", price.price)
}
}
fmt.Print("]")
fmt.Print("\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment