Random number in jekyll

Is there any way I could have a random number in jekyll?

jekyll is just building a static site, so it would only be generated when you build the site so it wouldn’t be very random.

You would be better off using JS to do it.

I wanted to use a random number to do a random quote type thing, and used the number of posts in jekyll, then multiplied that by the day of the month and divided that by something and used liquids modulo function to give me a random 1 digit number.

Yes I wanted a random number only in build time. Using day of the month is a nice idea. However isnt there a built in function?

not that I found, but that was a couple years ago. If there is it would be a Liquid thing not jekyll.

Here is a write up of what I did then - I wanted it to generate multiple different times each being random:

http://ephotopros.com/2015/jekyll-randomizer-for-ads/

No built in random filter for Liquid or Jekyll, but there are some alternatives here… and links to reasoning why it’s not in core.

I followed your link and reached here:

It suggests to use something like {{ random:5 }} I don’t know if its implemented or not. What is a sample feature? :thinking:

At the end of the page you linked to, it shows that Jekyll thenceforth learnt a new filter sample that also returns a random entr(ies) from an array…


    def sample(input, num = 1)
      return input unless input.respond_to?(:sample)
      num = Liquid::Utils.to_integer(num) rescue 1
      if num == 1
        input.sample
      else
        input.sample(num)
      end
    end