Simulate Random Risk Battle For Prof. Yorgey's 2013 Haskell course, I'm working on a homework to simulate a Risk battle.

type Army = Int

data Battlefield = Battlefield { attackers :: Army, defenders :: Army } 
                    deriving (Show, Eq)


In addition, Prof. Yorgey implement the following code to simulate the random throwing of dice:

newtype DieValue = DV { unDV :: Int } 
  deriving (Eq, Ord, Show, Num)

first :: (a -&gt; b) -&gt; (a, c) -&gt; (b, c)
first f (a, c) = (f a, c)

instance Random DieValue where
  random           = first DV . randomR (1,6)
  randomR (low,hi) = first DV . randomR (max 1 (unDV low), min 6 (unDV hi))

die :: Rand StdGen DieValue
die = getRandom


The second homework question asks to implement the battle function:


  which simulates a single battle (as explained above) between two
  opposing armies. That is, it should simulate randomly rolling the
  appropriate number of dice, interpreting the results, and updating the
  two armies to reflect casualties. You may assume that each player will
  attack or defend with the maximum number of units they are allowed.


And then for my implementation:

battle :: Battlefield -&gt; Rand StdGen Battlefield
battle bf = return $ battleOneRound bf

battleOneRound :: Battlefield -&gt; Battlefield
battleOneRound bf = updateArmy bf (compete a_dice d_dice)
  where
    a_dice = rollDieN . getLegalAttackers $ (attackers bf)
    d_dice = rollDieN . getLegalDefenders $ (defenders bf)

rollDieN :: Army -&gt; [DieValue]
rollDieN n 
  | n &lt;= 0     = []
  | otherwise = evalRand die (mkStdGen (1000*n)) : rollDieN (n-1) 

type AttackersDice = [DieValue]
type DefendersDice = [DieValue]

-- defender must have at least 2 left at base
getLegalDefenders :: Army -&gt; Army
getLegalDefenders n 
  | n &gt;= 4    = 2
  | n == 3    = 1
  | otherwise = 0

-- attackers must have at least 1 left at base
getLegalAttackers :: Army -&gt; Army
getLegalAttackers n 
  | n &gt;= 4    = 3
  | n == 3    = 2
  | n == 2    = 1
  | otherwise = 0


Testing

ghci&gt; let bf = battle $ Battlefield 10 20

ghci&gt; (evalRand bf) $ mkStdGen 10
Battlefield {attackers = 8, defenders = 20}

ghci&gt; (evalRand bf) $ mkStdGen 5
Battlefield {attackers = 8, defenders = 20}

ghci&gt; (evalRand bf) $ mkStdGen 444444
Battlefield {attackers = 8, defenders = 20}


As you can see, the actual StdGen argument does not appear to have any impact on the battle results. That's because rollDieN uses mkStdGen (1000*n) to simulate random die throws.

Please critique this implementation. I'm not sure if I've achieved randomness in the battle function.
rollDieN doesn't look very random to me

*Risk&gt; map unDV $ rollDieN 100
[5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,
 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6]


This looks better:

*Risk&gt; map unDV $ evalRand (replicateM 100 die) (mkStdGen 123456)
[4,5,2,3,5,6,2,3,2,5,4,5,5,5,1,1,6,6,2,3,2,5,5,3,4,1,3,1,
 3,2,4,4,3,2,2,6,2,2,3,1,6,6,4,5,3,6,6,6,5,2,2,1,6,3,6,3,
 6,3,6,5,6,5,1,1,1,2,1,5,4,4,3,3,3,3,3,3,4,5,5,2,4,1,5,4,
 4,2,1,1,5,1,4,2,6,5,3,5,2,3,6,1]


Now we can write a function that will return the two army's dice rolls

rollDice n = replicateM n die

battleTest :: Battlefield -&gt; Rand StdGen ([Int], [Int])
battleTest battlefield = do
    attackerRolls &lt;- rollDice $ getLegalAttackers (attackers battlefield)
    defenderRolls &lt;- rollDice $ getLegalDefenders (defenders battlefield)
    return (map unDV attackerRolls, map unDV defenderRolls)


And check that our output looks sane

*Risk&gt; evalRand (battleTest (Battlefield 10 20)) (mkStdGen 12)
([6,3,2],[5,1])
*Risk&gt; evalRand (battleTest (Battlefield 10 20)) (mkStdGen 123456)
([4,5,2],[3,5])


Now assuming a function to update the battlefield based on dice rolls, we just change it to

battle :: Battlefield -&gt; Rand StdGen Battlefield
battle battlefield = do
    attackerRolls &lt;- rollDice $ getLegalAttackers (attackers battlefield)
    defenderRolls &lt;- rollDice $ getLegalDefenders (defenders battlefield)
    return $ nextBattlefield attackerRolls defenderRolls battlefield




There's also a problem with getLegalDefenders. The homework says


  The defending player may defend with up to two units (or only one if
  that is all they have).


So instead of


getLegalDefenders :: Army -&gt; Army
getLegalDefenders n 
  | n &gt;= 4    = 2
  | n == 3    = 1
  | otherwise = 0



It should look like

getLegalDefenders :: Army -&gt; Army
getLegalDefenders n 
  | n &gt;= 2    = 2
  | n == 1    = 1
  | otherwise = 0


Or

getLegalDefenders n = max 0 (min 2 n)