# Generate a random array of entire numbers using a range

**URL:** <https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028>\
**Category:** Ask for Help\
**Created:** [July 7, 2022, 6:37am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028 "2022-07-07T06:37:57Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 6:37am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/1 "2022-07-07T06:37:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 7:33am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/2 "2022-07-07T07:33:40Z")

</div>

Is the length of the array fixed?

You could use a series of [Dice Roll](https://www.glideapps.com/plugins/dice) 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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 7:56am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/3 "2022-07-07T07:56:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 8:27am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/4 "2022-07-07T08:27:24Z")

</div>

![Screen Shot 2022-07-07 at 4.24.57 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/d/fd8dec8b07d8ebef423ce957a34e627e77aee8cc.png)

```auto
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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 9:25am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/5 "2022-07-07T09:25:28Z")

</div>

> [@Darren\_Murphy](#):
>
> ```auto
> 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(',');
> 
> ```

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 ☹

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/e/2/e2c392c35646855c6f05301a2d7d469fe92226b4.png)

It seems it’s sorting by the first digit on each element.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 9:29am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/6 "2022-07-07T09:29:33Z")

</div>

> [@Daniel\_Rocha](#):
>
> it’s there a way to sort the elements in an ascending way?

Just change the last line as follows:

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

```

> [@Daniel\_Rocha](#):
>
> It seems it’s sorting by the first digit on each element.

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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 9:37am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/7 "2022-07-07T09:37:10Z")

</div>

> [@Darren\_Murphy](#):
>
> `return arr.sort().join(',');`

Maybe I’m doing something wrong?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/f/3f5094cb116f383dbe918b375a7a3b031eadb67a.png)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 9:37am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/8 "2022-07-07T09:37:33Z")

</div>

And when you click done…?

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 9:38am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/9 "2022-07-07T09:38:24Z")

</div>

Yes, the problem persist after that too.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 9:39am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/10 "2022-07-07T09:39:54Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 9:42am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/11 "2022-07-07T09:42:41Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/0/3004eee43f586b5367cbe9782530d9ea0e7c73c1.png)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 9:48am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/12 "2022-07-07T09:48:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 9:50am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/13 "2022-07-07T09:50:44Z")

</div>

> [@Darren\_Murphy](#):
>
> ```auto
> var sorted = arr.sort();
> return sorted.join(',');
> 
> ```

Mega appreciated it, thank you so much.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 7, 2022, 10:09am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/14 "2022-07-07T10:09:10Z")

</div>

Okay, here you go. This should work.

```auto
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(',');

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Rocha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_rocha/32/44526_2.png) [@Daniel\_Rocha](https://community.glideapps.com/u/Daniel_Rocha)\
**Post date:** [July 7, 2022, 10:28am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/15 "2022-07-07T10:28:21Z")

</div>

> [@Darren\_Murphy](#):
>
> ```auto
> 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(',');
> 
> ```

It works Perfectly THANK YOU SO MUCH!!! Master Darren 🙏 🙏 🙏 🙏 👏 👏 👏

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/d/2d01b5f3c8aa4a630a3ebe1b29c85dc41342c2d9.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [July 8, 2022, 10:28am UTC](https://community.glideapps.com/t/generate-a-random-array-of-entire-numbers-using-a-range/44028/16 "2022-07-08T10:28:32Z")

</div>

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