Skip to content

Instantly share code, notes, and snippets.

@cfr
Last active August 29, 2015 14:09
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 cfr/eb60df03439aa754ff2b to your computer and use it in GitHub Desktop.
Save cfr/eb60df03439aa754ff2b to your computer and use it in GitHub Desktop.
Simple torus distribution
function randr(min, max) {
return Math.random() * (max - min) + min;
};
function torusDistribPoint(radius, size) {
var l = randr(size - radius, size + radius);
var a = randr(0, 2*Math.PI);
return [l * Math.cos(a), l * Math.sin(a)];
}
all: plot
torus: torusdistr.hs
ghc --make torusdistr.hs
plot: torus
./torusdistr > data
gnuplot plot.gpl
set terminal png size 400,400 enhanced font "Helvetica,6"
set output 'torus.png'
set autoscale
plot "data" using 1:2
{-# LANGUAGE ScopedTypeVariables #-}
import System.Random (randomR, getStdGen, StdGen)
import Control.Monad (mapM_, replicateM)
import Control.Monad.State
torusDist gen n radius size = transform $ zip xs ys
where (xs, gen') = randoms n (0, 2*pi) gen
(ys, _) = randoms n (size - radius, size + radius) gen'
transform :: Floating a => [(a, a)] -> [(a, a)]
transform [] = []
transform ((x, y):ts) = (a, b) : transform ts
where a = y * sin x
b = y * cos x
randoms n i = runState (replicateM n (state (randomR i)))
main = do gen <- getStdGen
let points :: [(Double, Double)] = torusDist gen 100 0.1 0.5
mapM_ print points
where print (x, y) = put x >> put y >> putStr "\n"
put x = putStr (show x) >> putStr " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment