# Generate a random array of entire numbers using a range

Hi guys, I’m trying to set a column in order to get an array (like a vector) of entire unique numbers… something like this (12, 22, 34,49,52,60)
After a quick investigation, I saw the Hyperformula option where we could set excel formulas in glide…
I tried to set the RANDARRAY formula but it seems it´s not supported by Hyperformula yet I wonder if any of you guys have done this before or if you have any ideas for it.

Is the length of the array fixed?

You could use a series of Dice Roll columns, and then join them together with a MakeArray column.

Or an alternative could be to use a JavaScript column and use a short JS snippet to return an array of random numbers.

Yes, it´s a fixed array like the example (2,4,8,14,17,34) and they shouldn’t get repeated.
I think if I use the dice roll then the number could get repeated like (2,2,4,8,5,9) and that is a problem for me.

And if I’m myself clear and transparent I haven’t used or studied how the JavaScrip works I don´t know if you could help me with an example of this JS snippet code you mentioned… mean while I’m going to investigate by my side about it too.

``````var arr = [];
while (arr.length < 6) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) {
arr.push(r);
}
}
return arr.join(',');
``````

One thing to note is that you can’t pass or return arrays to/from the JavaScript column. So it has to be returned as a string. But that can be coerced into an array by using a Split Text column if that’s what you actually need.

3 Likes

WOOW, that´s awesome I really appreciate your help that´s exactly what I was looking for.

I don´t wanna be annoying but… it’s there a way to sort the elements in an ascending way?

I tried with the sort array column but it doesn’t work It seems it’s sorting by the first digit on each element.

Just change the last line as follows:

``````return arr.sort().join(',');

``````

yeah, that’s an issue that’s been around for a while.
When you create an array of numbers using Split Text, you actually get an array of strings that look like numbers. So if you then subsequently try to sort the array using the Sort Array plugin, it does a lexical sort rather than a numerical sort.

Maybe I’m doing something wrong?

And when you click done…?

Yes, the problem persist after that too.

That’s odd. It works for me. Anyway, try this:

``````var sorted = arr.sort();
return sorted.join(',');
``````

It works perfectly when 2 digits numbers show up like 10,22,56,53,62,70
other wise…

Oh, looks like JS is also doing a lexical sort. Will need a custom sorting function. I’m away from my keyboard for a short while. Will come back with a solution when I get back.

Mega appreciated it, thank you so much.

Okay, here you go. This should work.

``````var arr = [];
while (arr.length < 6) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) {
arr.push(r);
}
}
var sorted = arr.sort(function(a,b) { return a-b });
return sorted.join(',');
``````
2 Likes

It works Perfectly THANK YOU SO MUCH!!! Master Darren       1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.