Fill A Table With Random Numbers (For A Beginner!)

Hey everyone, just started using Retool and absolutely love it. Have been able to build some amazing SQL tables and buttons etc to help with our business and so far really impressed.

I am stuck on the next project I am trying to create with something that will probably be very simple for most of you retool experts.

Basically I want to provide 3 inputs - Minimum, Maximum & Total

I would like to fill a table with (Total) random values between the (Minimum) and (Maximum)

So for example 100 rows of random numbers between 1 and 5000.

Any help would be wonderful

Thanks all

I think you just need to put some min/max values into

Math.random() * (max - min) + min;

There's some deep Stack Overflow discussions about optimizing the randomness and ensuring only integer values are returned, but I think you could just loop this 100 times to insert records into your DB

Thanks for replying Pyrrho! I did find similar code online, the problem I have is I cant work out how I can populate a table with this data? Do I use a transformer or just a JS snippet of code and how do I get it to populate the table? Sorry if this sounds so basic!

Thanks

There are a couple of ways to approach this, I think.

One would be to build it into a transformer that returns your results. Like this for the numbers:

Then you would tie the output to the query which would write everything to your DB. You'd probably want to do more than just return an array of integers for this.

Another way would be to build this out as a Retool Workflow instead of an App.

There you could build the same dataset and then send it through a loop node to write values to your DB based on the dataset:

In a loop node {{value}} is the current item in the loop from the code block.

2 Likes

Thanks. Code works fantastic in the output window.

I dont want this written to a db though, I just want a table with all of these numbers in it, each in its own row (1 column)

When I add a table and choose the data source as query2 it just says No data and doesnt populate with the numbers array. I must be missing something!

Thank you so much for helping me with this.

If you haven't yet, be sure to click the Run button on the query rather than just the Preview button. If you see output data in the query but the table says No Data this is most likely what is happening.

If so, it'd just be a matter of running the queries on page load or having a button that refreshes the table data (runs the javascript).

ETA:
Instead of just pushing numbers back to the array, push the values to an object (code line 6):

This allows you to regen the columns and map to the item values rather than trying to parse out just the values.


I must be doing something wrong! The table says 100 results, the code is the same as yours but its not displaying anything? What have I done or need to do?

I think it's the bit I added as a part of my last reply!

Make the numbers array push an object instead of just the numbers:

numbers.push({number: Math.floor(Math.random() * (max - min) + min)})

Then the table should be able to identify the number property of the array values.

2 Likes

Thank you so so much pyrrho! I totally missed that part but does make sense in my javascript beginner head! This is perfect! So grateful!

1 Like