Created
October 28, 2015 18:49
-
-
Save mjohnson9/017f252dd0e10aeba447 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 ( | |
cryptorand "crypto/rand" | |
"fmt" | |
"image" | |
"image/color" | |
"image/png" | |
"math/big" | |
"math/rand" | |
"os" | |
"time" | |
) | |
const ( | |
MIN_X = 0 | |
MIN_Y = 0 | |
MAX_X = 512 | |
MAX_Y = 512 | |
) | |
func createMathRandom() { | |
fmt.Print("Generating math.png...\n") | |
mathRandImage := image.NewGray(image.Rectangle{ | |
Min: image.Point{X: MIN_X, Y: MIN_Y}, | |
Max: image.Point{X: MAX_X, Y: MAX_Y}, | |
}) | |
for x := MIN_X; x <= MAX_X; x++ { | |
for y := MIN_Y; y <= MAX_Y; y++ { | |
generatedInt := rand.Intn(2) | |
var generatedColor uint8 | |
if generatedInt == 0 { | |
generatedColor = 0 | |
} else { | |
generatedColor = 255 | |
} | |
mathRandImage.SetGray(x, y, color.Gray{Y: generatedColor}) | |
} | |
} | |
mathImageFile, err := os.Create("math.png") | |
if err != nil { | |
fmt.Printf("Failed to open math.png: %s\n", err) | |
return | |
} | |
defer mathImageFile.Close() | |
err = png.Encode(mathImageFile, mathRandImage) | |
if err != nil { | |
fmt.Printf("Failed to write math.png: %s\n", err) | |
return | |
} | |
fmt.Print("Generated math.png\n") | |
} | |
var ( | |
CRYPTO_MAX = big.NewInt(2) | |
CRYPTO_ZERO = big.NewInt(0) | |
) | |
func createCryptoRandom() { | |
fmt.Print("Generating crypto.png...\n") | |
cryptoRandImage := image.NewGray(image.Rectangle{ | |
Min: image.Point{X: MIN_X, Y: MIN_Y}, | |
Max: image.Point{X: MAX_X, Y: MAX_Y}, | |
}) | |
for x := MIN_X; x <= MAX_X; x++ { | |
for y := MIN_Y; y <= MAX_Y; y++ { | |
generatedInt, err := cryptorand.Int(cryptorand.Reader, CRYPTO_MAX) | |
if err != nil { | |
fmt.Printf("Failed to generate a random number for crypto.png: %s\n", err) | |
return | |
} | |
var generatedColor uint8 | |
if generatedInt.Cmp(CRYPTO_ZERO) == 0 { | |
generatedColor = 0 | |
} else { | |
generatedColor = 255 | |
} | |
cryptoRandImage.SetGray(x, y, color.Gray{Y: generatedColor}) | |
} | |
} | |
cryptoImageFile, err := os.Create("crypto.png") | |
if err != nil { | |
fmt.Printf("Failed to open crypto.png: %s\n", err) | |
return | |
} | |
defer cryptoImageFile.Close() | |
err = png.Encode(cryptoImageFile, cryptoRandImage) | |
if err != nil { | |
fmt.Printf("Failed to write crypto.png: %s\n", err) | |
return | |
} | |
fmt.Print("Generated crypto.png\n") | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
createMathRandom() | |
createCryptoRandom() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment