Silly question about random function

Started by Vincent, Sun 31/08/2014 22:02:25

Previous topic - Next topic

Vincent

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.

Khris


Vincent

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

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 ?

Khris

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.

Vincent

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

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

monkey0506

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

Vincent

Many thanks monkey_05_06 for giving a helping hand, gracious

Vincent

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

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 ?

Khris

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
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:
Code: ags
  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:
Code: ags
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);
}

Vincent

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

SMF spam blocked by CleanTalk