 # Why is the result undefined?

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.

here is my random weighted number!

``````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)

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

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

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

I rewrote the code in a far better way!

``````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++) {
}
}
return numbers.get(floor(random(numbers.size())));
}
``````

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

ok, I’ll do it in a few hours

``````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
}
}
//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.
}
``````
1 Like

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

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

1 Like

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

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

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

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

What do you usually code in your free time?

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.

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

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

Don’t you have very little time left for everything