Forum Discussion

domladden's avatar
9 years ago

Random number generator

Hi, FreeMarker doesn't have a random number generator for some reason so has anyone created one before?

 

I want to display random posts depending on topic so I can create a more dynamic page.

 

For instance, I want to pull all posts labelled 'Test' and display 3 of these at random.

 

I don't think I can do this via JS because I have to call the total number of posts with that label and then generate 3 random numbers within that range, then display those specific posts.

 

As far as my admittedly limited experience with JS goes, because all of this happens server side the JS can't run at the correct point in this sequence.

 

I found this random number generator: http://freestyle-developments.co.uk/blog/?p=327 which does work but only to a point, in that it's not entirely random, if you use a range of 1 to 10 and run it 10 times, almost half return 1 so I don't think it's accurate at all. Even if you put in a range of 1 to 25000 you still get 1 almost every other time.

 

All I want is something similar to JS Math.random(min, max) but can't find a simple way of doing this at all.

 

Can anyone help?

 

Thanks

  • The utils object has some utilities for generating random numbers. Try utils.numbers.randomIntWithCeiling(ceiling)

  • ChristineC's avatar
    ChristineC
    Khoros Alumni (Retired)

    The utils object has some utilities for generating random numbers. Try utils.numbers.randomIntWithCeiling(ceiling)

    • domladden's avatar
      domladden
      Ace
      Hi Christine, is there any way for this to be used more than once at once, so to speak?

      for instance :

      Random 1 = ${utils.numbers.randomIntWithCeiling(25)}
      Random 2 = ${utils.numbers.randomIntWithCeiling(25)}
      Random 3 = ${utils.numbers.randomIntWithCeiling(25)}

      Will return the same number for all 3 variables.

      The ceiling is the number of posts I have, and I want 3 random posts.

      I tried

      Random1 = ${utils.numbers.randomIntWithCeiling(TotalPosts)}
      Random2 = ${utils.numbers.randomIntWithCeiling(TotalPosts -1)}
      Random3 = ${utils.numbers.randomIntWithCeiling(TotalPosts -2)}

      And that works in that random numbers are generated but there are a few pitfalls in that approach like causing duplicates.

      I will obviously have to account for duplicates etc. anyway but still, if I can keep the approach as clean as possible I would prefer that.

      Thanks for your help with the original request though that has helped a lot.
      • ChristineC's avatar
        ChristineC
        Khoros Alumni (Retired)

        Got some help from DougS. It looks like the freemarker library is doing that caching. As a workaround, you can try something like this

        Random 1 = ${utils.numbers.randomIntWithCeiling(25)}
        Random 2 = ${(utils.numbers.randomIntWithCeiling(26) - 1)?abs}
        Random 3 = ${(utils.numbers.randomIntWithCeiling(27) - 2)?abs}