Last active
August 29, 2015 13:57
-
-
Save lintianzhi/9828096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"strings" | |
"time" | |
) | |
var ( | |
ra *rand.Rand | |
) | |
func main() { | |
ra = rand.New(rand.NewSource(time.Now().Unix())) | |
text() | |
} | |
func text() { | |
fmt.Println("Muspi Merol") | |
pNum := randRange(5, 20) | |
for i := 0; i < pNum; i++ { | |
paragraph() | |
} | |
} | |
func paragraph() { | |
sNum := randRange(3, 7) | |
lineLen := 0 | |
for i := 0; i < sNum; i++ { | |
lineLen = sentence(lineLen) | |
} | |
fmt.Println() | |
} | |
func sentence(lineLen int) int { | |
wNum := randRange(3, 15) | |
for i := 0; i < wNum; i++ { | |
w, wLen := word(i == wNum-1) | |
if i == 0 { | |
w = strings.Title(w) | |
} | |
if lineLen+wLen > 80 { | |
fmt.Printf("\n%s", w) | |
lineLen = wLen | |
} else { | |
if lineLen == 0 { | |
fmt.Printf("%s", w) | |
lineLen += wLen | |
} else { | |
fmt.Printf(" %s", w) | |
lineLen += wLen + 1 | |
} | |
} | |
} | |
return lineLen | |
} | |
var wBuf = make([]byte, 10) | |
var percentV = 5 | |
var vowels = []byte{'p', 'o', 't', 'd'} | |
func word(isLast bool) (string, int) { | |
len := randRange(1, 11) | |
for i := 0; i < len; i++ { | |
if randRange(0, 100) < percentV { | |
wBuf[i] = vowels[randRange(0, 4)] | |
} else { | |
wBuf[i] = 'a' + byte(randRange(0, 26)) | |
} | |
} | |
if isLast { | |
return string(wBuf[:len]) + ".", len + 1 | |
} | |
return string(wBuf[:len]), len | |
} | |
func randRange(from, to int) int { | |
d := ra.Int() % (to - from) | |
return from + d | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment