Skip to content

Instantly share code, notes, and snippets.

@Alanaktion
Last active January 12, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alanaktion/8180ce346e084ba5f59a77353d99369b to your computer and use it in GitHub Desktop.
Save Alanaktion/8180ce346e084ba5f59a77353d99369b to your computer and use it in GitHub Desktop.
Generate English-like random paragraphs
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func main() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// https://en.wikipedia.org/wiki/Letter_frequency
vowels := "aaaaaaaaeeeeeeeeeeeeiiiiiiiooooooouuu"
consonants := "qwwrrrrrrtttttttttyyppssssssddddffgghhhhhhjkllllzxcccvbnnnnnnnmm"
minWordLen := 2
maxWordLen := 7
sentenceCount := r.Intn(5) + 3
paragraph := ""
for s := 0; s < sentenceCount; s++ {
wordCount := r.Intn(6) + 4
sentence := ""
for w := 0; w < wordCount; w++ {
wordLen := r.Intn(maxWordLen+minWordLen) + minWordLen
for c := 0; c < wordLen; c++ {
var char byte
if c%2 == 0 {
char = consonants[r.Intn(len(consonants)-1)]
} else {
char = vowels[r.Intn(len(vowels)-1)]
}
if w == 0 && c == 0 {
sentence += strings.ToUpper(string(char))
} else {
sentence += string(char)
}
}
sentence += " "
}
paragraph += strings.Trim(sentence, " ") + ". "
}
fmt.Println(paragraph)
}
#!/usr/bin/php
<?php
# https://en.wikipedia.org/wiki/Letter_frequency
$vowels = 'aaaaaaaaeeeeeeeeeeeeiiiiiiiooooooouuu';
$consonants = 'qwwrrrrrrtttttttttyyppssssssddddffgghhhhhhjkllllzxcccvbnnnnnnnmm';
$minWordLen = 2;
$maxWordLen = 7;
$sentenceCount = rand(3, 8);
$paragraph = '';
for ($s = 0; $s < $sentenceCount; $s++) {
$wordCount = rand(4, 10);
$sentence = '';
for ($w = 0; $w < $wordCount; $w++) {
$wordLen = rand($minWordLen, $maxWordLen);
for ($c = 0; $c < $wordLen; $c++) {
if ($c % 2 == 0) {
$char = str_shuffle($consonants)[0];
} else {
$char = str_shuffle($vowels)[0];
}
$sentence .= $char;
}
$sentence .= ' ';
}
$paragraph .= ucfirst(trim($sentence)) . '. ';
}
echo $paragraph, "\n";
#!/usr/bin/python
import random
# https://en.wikipedia.org/wiki/Letter_frequency
vowels = 'aaaaaaaaeeeeeeeeeeeeiiiiiiiooooooouuu'
consonants = 'qwwrrrrrrtttttttttyyppssssssddddffgghhhhhhjkllllzxcccvbnnnnnnnmm'
minWordLen = 2
maxWordLen = 7
sentenceCount = random.randint(3, 8)
paragraph = ''
for s in range(sentenceCount):
wordCount = random.randint(4, 10)
sentence = ''
for w in range(wordCount):
wordLen = random.randint(minWordLen, maxWordLen)
for c in range(wordLen):
if c % 2 == 0:
char = random.choice(consonants)
else:
char = random.choice(vowels)
sentence += char
sentence += ' '
paragraph += sentence.strip().capitalize() + '. '
print(paragraph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment