Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Last active December 18, 2015 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/5749703 to your computer and use it in GitHub Desktop.
Save joseanpg/5749703 to your computer and use it in GitHub Desktop.
[Pendiente]
-- Basado en
-- https://twitter.com/joseanpg/status/343757288089190401
-- https://twitter.com/joseanpg/status/343758742455738368
-- https://twitter.com/joseanpg/status/343759216424652802
-- Version ultramejorable: http://t.co/da8tx1LK6r
-- Versión Clojure by @jneira: https://gist.github.com/jneira/5745669
-- con probabilidad inversamente proporcional a las prioridades
import Random
import Control.Monad
getindex :: [Float] -> Float -> Int
getindex xs v = alpha xs (sigma * v ) 0
where sigma = sum xs
alpha [] _ i = i
alpha (y:ys) a i = let a' = a - y
in if a' < 0 then i
else alpha ys a' (i+1)
enjoy gen xs = let zet = getindex xs
in gen >> randomRIO (0,1) >>= return . zet >>= print
main = forever (enjoy newStdGen [20,15,5])
-- Si se impone como condición que los pesos sean naturales, no es necesario salir de la artimética Int
import Random
import Control.Monad
getindex :: [Int] -> Int -> Int
getindex xs v = alpha xs (sigma * v ) 0
where sigma = sum xs
alpha [] _ i = i
alpha (y:ys) a i = let a' = a - y
in if a' < 0 then i
else alpha ys a' (i+1)
enjoy gen xs = let zet = getindex xs
in gen >> randomRIO (0,1) >>= return . zet >>= print
main = forever (enjoy newStdGen [20,15,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment