# Function to return int( from beginner) stuck

**URL:** <https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871>\
**Category:** Beginners\
**Created:** [January 27, 2019, 5:49am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871 "2019-01-27T05:49:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)\
**Post date:** [January 27, 2019, 5:49am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871/1 "2019-01-27T05:49:33Z")

</div>

This is my code. so it prints out the random numbers. But what I want is; like rate1 = first call to RateChoose();

rate2 = second call to RateChoose();  
rate3 = third call

```auto
void RateChoose(){int rate0 = 0;
  for (int i= 1; i<7; i++)
  { 
    //cho0se a number between 0 and 9
    int rate = int(random(10));
    // println(rate);
    rate0 = rate0*10 + rate ;
  }  
  println (rate0);
}

{
  RateChoose();
  RateChoose();
  RateChoose();
}

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [January 27, 2019, 6:00am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871/2 "2019-01-27T06:00:34Z")

</div>

I‘m not sure if i got you right, but i think you would need to make rate0 into an array and call the respective int in the array inside the Function :

```auto
void rateChoose(int i) {
   int tempRate = 0;
   for (int i = 0; i < 7; i++) {//i don‘t know why you need this, but i‘ll just leave it
      tempRate = tempRate * 10 +int(random(10));
   }
   rate[i] = tempRate;
}

rateChoose(0);
rateChoose(1);
rateChoose(2);

```

or

```auto
void rateChoose() {
   int tempRate;
   for (int i = 1; i< 7; i++) { //again, don‘t know why the for loop...
      tempRate =tempRate* 10+ int(random(10));
   }
   return tempRate;
}
rate[0] = rateChoose();
rate[1] = rateChoose();

```

This would do what you want… If i got you right. There is a way to make it easier, but i don‘t know the context, so i can‘t Tell what could be changed and what wouldn‘t Work. I could help more if i knew what you want to do exactly…  
Anyway, Hope this helps ^^

---

<div class="post-metadata">

**Author:** ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)\
**Post date:** [January 27, 2019, 7:49am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871/3 "2019-01-27T07:49:00Z")

</div>

this is my function; I want to replace the random 100000 with a 6 digit call to random 0-9 . I thought it would be easy but cant get my head around the function call.

```auto
void draw() { listOfRates();
noLoop();
}

void listOfRates() {String[] lines = loadStrings("listin.txt");
String[]rates =new String[lines.length];
println("there are " + lines.length + " lines");
//print(lines);
for (int i = 0 ; i < lines.length;) {
int rate1 = int(random(100000));
int rate2 = int(random(100000));
int rate3 = int(random(100000));
//println(i);
 println(lines[i] + " " + rate1 +" " + rate2 +" " + rate3 );
 rates[i] = lines[i] + " " + rate1 +" " + rate2 +" " + rate3 ;

 i++;

}
saveStrings("listout.txt",rates); 

}

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [January 27, 2019, 10:57am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871/4 "2019-01-27T10:57:21Z")

</div>

Oh ok, i think i got it now, but why? random(100000) is exactly the same as Setting each digit to be random… anyway, in this case you can just do this :

```auto
int rateChoose() {
   int result;
   for (int i = 0; i < 6; i++) {
      result = result*10+int(random(10));
   }
   return result;
}

{
int rate1 = rateChoose();
int rate2 = rateChoose();
}

```

You can also make it customizable by adding parameters like… instead of having a 6digit number you could do :

```auto
int rateChoose(int digitLength) {
   for (int i = 0; i < digitLength; i++) {
   ....
   }
....
}

```

Or

```auto
int rateChoose(int maxDigit) {
   for (int i = 0; i < 6; i++) {
      result = result * 10 + int(random(maxDigit));
   }
}

```

So that if maxDigit is 8, the highest number possible is 888888 and you can‘t get any number with a 9 in it.

I still don‘t know why you would need that, but Hope this helps ^^

---

<div class="post-metadata">

**Author:** ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)\
**Post date:** [January 27, 2019, 11:58am UTC](https://discourse.processing.org/t/function-to-return-int-from-beginner-stuck/7871/5 "2019-01-27T11:58:41Z")

</div>

thank you. That is great. Simple and sweet. I would not have been able to work it out from the examples and info on the processing page. just for your info. It was like to simulate dialing on a phone. choose a random number ,then dial that, then next then dial that rather than choose all the numbers first then dial them. I am sure I did that as a kid . wondering who i would dial up.
