Function to return int( from beginner) stuck


#1

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

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();
}

#2

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 :

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

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


#3

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.

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

}

#4

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 :

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 :

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

Or

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


#5

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.