Liquid: successive random numbers excluding the first results | Community
Skip to main content

Liquid: successive random numbers excluding the first results

  • October 22, 2022
  • 1 reply
  • 0 views

The purpose of the code is to ask random security questions using Liquid. I used the following code which creates a random number which is then replaced by a question.

{% assign min = 1 %}
{% assign max = 7 %}
{% assign diff = max | minus: min %}
{% assign randomNumber1 = "now" | date: "%N" | modulo: diff | plus: min %}

{{randomNumber1 | replace: "1", "Question 1" | replace: "2", "Question 2" | replace: "3", "Question 3" | replace: "4", "Question 4"| replace: "5", "Question 5"| replace: "6", "-Question 6"| replace: "7", "Question 7"}}

I copied this code three times to have three numbers/questions (with randomNumber1, randomNumber2, randomNumber3). The tricky part is that a question should not appear twice.

For that randomNumber2 must never be equal to randomNumber1 and randomNumber3 must never be equal to randomNumber1 and randomNumber2. Unfortunately I have not found an ideal solution to code these conditions.

Do you have one maybe? Thanks :)

1 reply

Tipene
  • October 28, 2022

Hey @agubuwexe,

In case you don't receive any suggestions here, I've dropped a link to the Liquid docs below that you might find helpful:

https://shopify.github.io/liquid/

Thanks!

Tipene