Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Vincent on Sun 31/08/2014 22:02:25

Title: Silly question about random function
Post by: Vincent on Sun 31/08/2014 22:02:25
Good evening to all AGSer.
I can feel a slight fright in asking this silly question and I apologize in ahead for this has been asked.
Because it looks so basic notion.

Referring to the (Random Function) into the manual says:
Returns a random number between 0 and MAX.
NOTE: The range returned is inclusive - ie. if you do Random(3); then it can return 0, 1, 2 or 3.

So the question was;
How do i do a random function that never return 0 ?
ie. if i do Random(3); then it can return 1, 2 or 3.

Still deeply sorry for asking this, thanks guys in advance.
Title: Re: Silly question about random function
Post by: Khris on Sun 31/08/2014 22:04:27
Random(2) + 1
Title: Re: Silly question about random function
Post by: Vincent on Sun 31/08/2014 22:24:42
Ciao Khris, Thank you very much for response.

So; Random(2) + 1 it will never return 0 !
i was wondering to create a function similar to the Random one in AGS, with the difference that never returns to 0.

Code (ags) Select

function NumeriCasuali(int minimo, int massimo) //int min, int max
{
  return (minimo + Random(massimo - minimo)); //wrong
}


the function apparently works well, but after some time, it happened that it returns to zero again.
how can I do to avoid this ?
Title: Re: Silly question about random function
Post by: Khris on Sun 31/08/2014 22:53:30
The function is fine; if minimo < massimo && minimo != 0, it will not return 0, ever.
If that happened, you're either using bad parameters or made a mistake somewhere else.
Title: Re: Silly question about random function
Post by: Vincent on Sun 31/08/2014 23:23:49
Exactly ! I just noticed a huge mistake on my part watching again the room script.
It's relatively funny because I wrote explicitly that my integer firstcounter = 0;

Code (ags) Select

bool pushed, released;
int time, firstcounter;

function room_RepExec()
{   
  if (time > 5) { gTarget.BackgroundGraphic = 3; firstcounter = NumeriCasuali(1, 5); }

  else if (released && time > 120)
   {
      firstcounter = 0; // Why The Hell... !!?
      Labelcounter.Text = "Time Out";
      Labelhit.Text = "You Failed";
      gTarget.BackgroundGraphic = 2;
      time = 0;
   }
   Labelinfo.Text = String.Format("%d %", time);
   time ++;
}


Thanks Khris for the clarification and suggestion.
Cordial greetings
Title: Re: Silly question about random function
Post by: monkey0506 on Mon 01/09/2014 20:57:01
Quote from: Khris on Sun 31/08/2014 22:53:30
The function is fine; if minimo < massimo && minimo != 0, it will not return 0, ever.
If that happened, you're either using bad parameters or made a mistake somewhere else.

Khris is exactly right about this. Seeing as AGS allows recursion though, it's pretty simple to error-proof your function:

Code (ags) Select
int NumeriCasuali(int minimo, int massimo) //int min, int max
{
  if (minimo == massimo) return minimo; // same value, only one possible result
  if (minimo > massimo) return NumeriCasuali(massimo, minimo); // min and max reversed, swap them out
  return (minimo + Random(massimo - minimo)); // correct
}
Title: Re: Silly question about random function
Post by: Vincent on Tue 02/09/2014 14:03:14
Many thanks monkey_05_06 for giving a helping hand, gracious
Title: Re: Silly question about random function
Post by: Vincent on Thu 04/09/2014 15:39:34
Hello Guys, I'm really sorry for bothering you again :(
I would like to generate the similar Function or Int "NumeriCasuali" in Float numbers..

Code (ags) Select

float RandomFloat(float min, float max)
{
  if (min == max) return min; // same value, only one possible result
  if (min > max) return RandomFloat(max, min); // min and max reversed, swap them out
  return (min + Random(FloatToInt(max)-FloatToInt(min))); //type mismatch: cannot convert 'float' to 'int'
}


How to do that ?
Title: Re: Silly question about random function
Post by: Khris on Thu 04/09/2014 18:47:57
The error is due to the first min, you need to also use FloatToInt() for that, then you need to IntToFloat() the entire expression, since the function is declared as returning a float.

However, this defeats the entire purpose of using Floats in the first place, since the returned value won't have any decimals.
There's an easy fix though:

Code (ags) Select
float RandomDecimals(int n, int d) {
  n--;
  int i;
  while (i < d) {
    n = n * 10 + Random(9);
    i++;
  }
  return IntToFloat(n) / Math.RaiseToPower(10.0, IntToFloat(d));
}


Now you can do this:
  float f = RandomDecimals(NumeriCasuali(10, 20), 3);
This should assign a value from somewhere between 10.000 to 19.999 to f.

Another way is this:
float RandomFloat(float max) {
  int huge = 2147483647;
  float f = IntToFloat(Random(huge)) / IntToFloat(huge);  // 0.0 - 1.0
  return f * max;
}

float RandomFloatRange(float min, float max) {
  if (min == max) return min;
  if (min > max) return RandomFloatRange(max, min);
  return min + RandomFloat(max - min);
}
Title: Re: Silly question about random function
Post by: Vincent on Thu 04/09/2014 20:18:50
I cannot find fair words to thank you and for your patience too.

I'm not even a religious man
(i don't know about you)
But God must bless you.
You are a Great Master.

Thank you again
For giving me this help, I appreciated

Cordial greetings :)