Jekyll, Liquid, Random numbers
This is choosing a random quote from a JSON file in _data but the principle should work with your posts as well:
{% assign random = site.time | date: "%s%N" | modulo: site.data.inspirational-quotes.size %}
<blockquote>“{{ site.data.inspirational-quotes[random].quote }}” <cite>{{ site.data.inspirational-quotes[random].person }}</cite></blockquote>
I found this article to be useful to generating numbers using Liquid, it is not straight forward, nonetheless, it is the most elegant way to generate a random number with min/max.
The following example is for a number between 65 & 80.
{% assign min = 65 %}
{% assign max = 80 %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}
I had a similar idea for a web site I am working on and resorted to write a plugin (see below). As Peter pointed out, the random selection will happen at generation time, so if you are looking at something dynamic you will have to look elsewhere.
Anyhow, this in the plugin I wrote (I placed it in my _plugins directory, eg. .../_plugins/randomPage.rb):
# Outputs a random page link
#
# Usage:
# {{ site.pages | random_page }}
# {{ site.collection_name | random_page }}
# {% assign myPage = site.collection_name | random_page %}
# <a href="{{ myPage }}">{{myPage}}</a>
# {% assign myPage = site.pages | random_page %}
# <a href="{{ myPage }}">{{myPage}}</a>
module RandomPageSelector
def random_page( input )
index = rand(0...input.length)
"#{input[index].url}"
end
end
Liquid::Template.register_filter(RandomPageSelector)