# Why is the result undefined?

**URL:** https://discourse.processing.org/t/why-is-the-result-undefined/26430
**Category:** Coding Questions
**Created:** [December 21, 2020, 9:43pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430 "2020-12-21T21:43:25Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 21, 2020, 9:43pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/1 "2020-12-21T21:43:25Z")

</div>

[https://www.openprocessing.org/sketch/1048482](https://www.openprocessing.org/sketch/1048482) this is my code.  
I’m trying to make a weighted random function where the position of the weight in the array is the result.  
I want to be able to put three numbers in for example 1, 1, 3, and receive a placement based on weight so output 3 should be the highest output 2, and 1 would be the same.

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 21, 2020, 10:00pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/2 "2020-12-21T22:00:14Z")

</div>

here is my random weighted number!

```auto
String options[] = {"0","1","2"};
int weight[] = {2,6,1};
int tWeight = 0;

void setup() {
  for(int i = 0; i < options.length; i++) {
  tWeight += weight[i];
}
}

void draw() {
  background(0);
  
}
void mousePressed() {
  getRandom();
}

String getRandom() {
  String a = "";
  int rand = floor(random(tWeight+0.999));
  println("rnd num" , rand);
  for(int i = 0; i < options.length; i++) {
    rand -= weight[i];
    if(rand <= 0) {
      a = options[i];
      println("result:",a);
      i = options.length +10;
    }
  }
  return a;
}

```

in this case 0 has a weight of 2, 1 has a weight of 6 and 2 is has a weight of 1.  
2/9 times it returns 0, 6/9 times it returns 1, 2/9 times it returns 2.  
(9 is because 2 + 6 + 1 = 9)

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 22, 2020, 11:37am UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/3 "2020-12-22T11:37:07Z")

</div>

Thx, does this work in p5.js or will I have to change it around?

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 22, 2020, 11:45am UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/4 "2020-12-22T11:45:17Z")

</div>

it works but the functions are named differently. Otherwise the logic is the same

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 22, 2020, 11:57am UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/5 "2020-12-22T11:57:40Z")

</div>

I re designed my based on yours using two arrays and it works except when it should be returning the first value in an array. so instead of 1 it returns undefined, that’s all but why? [https://www.openprocessing.org/sketch/1048482](https://www.openprocessing.org/sketch/1048482)

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 22, 2020, 1:11pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/6 "2020-12-22T13:11:42Z")

</div>

I rewrote the code in a far better way!

```auto
int num[] = {1,2,3,4};
int weight[] = {1,3,7,1};
void draw() {
}
void mousePressed() {
  println(getRandom());
}

int getRandom() {
  ArrayList<Integer> numbers = new ArrayList<Integer>();
  for(int i = 0; i < num.length; i++) {
    for(int j = 0; j < weight[i]; j++) {
      numbers.add(num[i]);
    }
  }
  return numbers.get(floor(random(numbers.size())));
}

```

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 22, 2020, 11:54pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/7 "2020-12-22T23:54:41Z")

</div>

> [@CodeMasterX](#):
>
> [spoiler]```  
> int num = {1,2,3,4};  
> int weight = {1,3,7,1};  
> void draw() {  
> }  
> void mousePressed() {  
> println(getRandom());  
> }
> 
> int getRandom() {  
> ArrayList numbers = new ArrayList();  
> for(int i = 0; i \< num.length; i++) {  
> for(int j = 0; j \< weight[i]; j++) {  
> numbers.add(num[i]);  
> }  
> }  
> return numbers.get(floor(random(numbers.size())));  
> }
> 
> ```auto
> 
> ```

could you please comment out and explain what each part in the code does so I can translate it easier. thx

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 7:10am UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/8 "2020-12-23T07:10:30Z")

</div>

ok, I’ll do it in a few hours

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 10:11am UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/9 "2020-12-23T10:11:28Z")

</div>

```auto
int num[] = {0,1,2,3,4};
int weight[] = {0,1,2,3,4};
void draw() {
}
void mousePressed() {
  println(getRandom());
}

int getRandom() { // is the function you call in order to get a weighted random number
  ArrayList<Integer> numbers = new ArrayList<Integer>(); //an array which is filled with all of the numbers
  for(int i = 0; i < num.length; i++) {
    for(int j = 0; j < weight[i]; j++) { //ading weight[i] numbers to array list
      numbers.add(num[i]);
    }
  }
  //current array is (with weight[] and num[] being 0,1,2,3,4) 0 0s, 1 1, 2 2s, 3 3s, 4 4s, aka 1,2,2,3,3,3,4,4,4,4
  return numbers.get(floor(random(numbers.size()))); //this function picks a random number from the array
  //therefore #1 has a 1/10 (10%) of being picked, #2 - 2/10 20%, #3 - 3/10 30%, #4 - 4/10 40%
  //if the array was 1,1,1,3,5,5,2,7; #1 would have 3 in 7, #3 would have 1 in 7, #5 2 in 7, #2 1 in 7, #7 1 in 7.
}

```

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 1:46pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/10 "2020-12-23T13:46:56Z")

</div>

Thank you so much, I translated it and now that I understand how it works I find it a really good solution. \<3

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 1:55pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/11 "2020-12-23T13:55:32Z")

</div>

I first developed it on scratch about 3 years ago : P

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 1:56pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/12 "2020-12-23T13:56:45Z")

</div>

how old were u back then? We all started on scratch lol

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 2:31pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/13 "2020-12-23T14:31:07Z")

</div>

I first started scratch in 4th grade (~9-10 years old)

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 2:34pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/14 "2020-12-23T14:34:07Z")

</div>

so you’re around 13, do you want to code as a career or something (im 13)

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 2:40pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/15 "2020-12-23T14:40:55Z")

</div>

No, I’m 15, started highschool : P currently in a technical (electrotechnic- **computer science** ( I chose computer science) program.

edit: but I don’t like scratch 3.0. far too restrictive. Some things are better but most are worse

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 2:44pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/16 "2020-12-23T14:44:11Z")

</div>

What do you usually code in your free time?

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 2:48pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/17 "2020-12-23T14:48:03Z")

</div>

I have a few hobbies but all are time consuming. I like coding, drawing on a tablet, reading light novels,… all take huge amounts of time to do / get better at.

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 2:51pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/18 "2020-12-23T14:51:29Z")

</div>

I wish I had that many my parents always complain because i play too much. Anyways here’s what I made using the weighted random [https://www.openprocessing.org/sketch/1048449](https://www.openprocessing.org/sketch/1048449). I split the function up so It wouldn’t have to run every time I needed a new number. It’s supposed to be a knife.

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 23, 2020, 2:57pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/19 "2020-12-23T14:57:13Z")

</div>

Yeah but all these involve computers : P I still spend a lot of time on it.

---

<div class="post-metadata">

### Author: ![olaf000olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/olaf000olaf/32/11295_2.png) [@olaf000olaf](https://discourse.processing.org/u/olaf000olaf)
#### Post date: [December 23, 2020, 2:58pm UTC](https://discourse.processing.org/t/why-is-the-result-undefined/26430/20 "2020-12-23T14:58:39Z")

</div>

Don’t you have very little time left for everything

[Next page](https://discourse.processing.org/t/why-is-the-result-undefined/26430.md?page=2)
