# Generate random numbers in array and sort without predefined sort function

**URL:** <https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 30, 2019, 5:46pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055 "2019-10-30T17:46:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewbCoder](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@NewbCoder](https://discourse.processing.org/u/NewbCoder)\
**Post date:** [October 30, 2019, 5:46pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055/1 "2019-10-30T17:46:30Z")

</div>

Help! I’m trying to generate random numbers in two arrays and then merge them without the predefined function. Any suggestions would be great. Having issues converting between int [] and int…This is what i have so far:

```auto
void setup ()
{
  int n = 5;
  int m = 8;
  int [] one = new int [n];
  int [] two = new int [m];
  foo(one,two); 
  one = sort(one);
  two = sort(two);
  printArray(one);
  printArray(two);
}
int foo( int [] one, int [] two)
{
  for(int i = 0; i < one.length; i ++)
  {
    for( int j = 0; j < two.length; j ++)
    {
     int r = (int(random(50)));
     int [] random = {r};
     one = random;
     two = random;
     return r;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2019, 5:58pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055/2 "2019-10-30T17:58:55Z")

</div>

merge means you need to generate a third array the size of the 2 first arrays together. Then fill it with data from 1 and 2.

Then you for loop over each one separately and pick the smallest number. Then replace the number by a marker -1000 and repeat to fill #3

Homework?

---

<div class="post-metadata">

**Author:** ![NewbCoder](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@NewbCoder](https://discourse.processing.org/u/NewbCoder)\
**Post date:** [October 30, 2019, 6:18pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055/3 "2019-10-30T18:18:56Z")

</div>

Yes, thank you for the help. I’m confused involving generating random numbers in an array. I’m always getting convert errors. The goal is to merge two arrays that have random numbers and sort them without the predefined sort function. I should of been more clear.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2019, 6:45pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055/4 "2019-10-30T18:45:19Z")

</div>

Well first set up the arrays with random.

The sorting is coming later. It is connected with merging.

**Setting up the random arrays**

When you want the function to return a random array

```auto
int[] getRandomArray() { 
   int[] randomArray=new int[10];
   for(....) {
       .....
   }//for

   return randomArray; // !!!!!!!!!!!!!!!!!
}//function

```

and in setup():

`int[] one=getRandomArray();`

`int[] two=getRandomArray();`

You can give the length of the array as a parameter

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2019, 6:56pm UTC](https://discourse.processing.org/t/generate-random-numbers-in-array-and-sort-without-predefined-sort-function/15055/5 "2019-10-30T18:56:51Z")

</div>

> [@NewbCoder](#):
>
> int r = (int(random(50)));

that’s good.

put that in the array

see

> **[Arrays](https://www.processing.org/tutorials/arrays/)**
>
> How to store and access data in array structures.
