Random Number Generator

Options
brian
brian Administrator

ADMIN

edited February 2023 in ✂️ Snippets

Generate a random number with this snippet, using a Lambda function.

It features:

  • GET /random_number: an API that accepts two inputs, min (an integer) and max (an integer) and returns a random integer between or equal to them.

You can use this snippet to learn how to:

  • Use Lambda functions
  • Use Math functions

Feel free to add suggestions, requests, or questions in the comments!

Comments

  • pkramar
    pkramar Member
    Options

    How can I generate random numbers with 0(s) in the begging? (let´s say I need 3-digit numbers from 000-999)

    Thank you

  • Liz Anaya
    Liz Anaya Member, Administrator

    ADMIN

    Options

    Hey @pkramar! I build something that could possibly work for you.

    1. Used the Generate Random Number function
      1. Set the min integer to 0 and the max to 999
    2. Added a conditional statement to check if the number was between 10 and 99
      1. If yes, I used the concat filter to add a 0 in front of the random number
    3. Added another conditional statement to check if the number was between less than 10
      1. If yes, I used the concat filter to add 00 in front of the random number
    4. Returned the random number

    The potential issue that I see with this is that the concat filter turns the type number into the type text. So on random numbers that are being updated by the concat filter, turn to the type text.

    Check out the to_int, if you need to turn the random number into an integer.

    Let me know if this works for you. 😊