About Store Forum Documentation Contact
Donations:
309$/mo



Post Reply 
randomF
Author Message
1991mirec Offline
Member

Post: #1
randomF
hi esenthel

I don t know if it is a bug or not.. but can you please look at randomF.
because when i used randomF(1,2) no matter what the number was 1 but when i used randomF(1,5) it gave me random numbers from 1 to 5. is there some limit or something how many numbers has to be in between??
i don t know if it happened to somebody else too but this seems to be not right.
thank you.

ok never mind.. i thing i got it.. it gives numbers from1 to 4 not five.. i just figured,,, so it is from first number to last number minus one ..
(This post was last modified: 09-30-2013 02:56 PM by 1991mirec.)
09-30-2013 02:51 PM
Find all posts by this user Quote this message in a reply
SamerK Offline
Member

Post: #2
RE: randomF
Hi, I just did small test,

RandomF(1,2) ---> numbers between 1.0 to 1.9
RandomF(1,5) ---> numbers between 1.0 to 4.9
(I don't know the reason why the limit number not counted?)

whereas

Random(1,2) ---> gives either 1 or 2 (no fractions)
Random(1,5) ---> gives 1 to 5 (no fractions)
(This post was last modified: 09-30-2013 03:19 PM by SamerK.)
09-30-2013 03:16 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #3
RE: randomF
Code:
/******************************************************************************/
void InitPre()
{
   EE_INIT();
   App.flag|=APP_MAXIMIZABLE|APP_MINIMIZABLE|APP_RESIZABLE;
   flt vals[10000]; REPAO(vals)=RandomF(1, 2);
   flt min=vals[0], max=min;
   REPA(vals)
   {
      MIN(min, vals[i]);
      MAX(max, vals[i]);
   }
   Exit(S+min+' '+max);
}
bool Init()
{
   return true;
}
void Shut()
{
}
bool Update()
{
   if(Kb.bp(KB_ESC))return false;
   Gui.update();
   return true;
}
void Draw()
{
   D.clear();
   Gui.draw();
}
/******************************************************************************/
this code gave me output of 1.000 and 2.000
09-30-2013 08:57 PM
Find all posts by this user Quote this message in a reply
SamerK Offline
Member

Post: #4
RE: randomF
I ran your code and it gave me the correct range 1 to 2, but check this code it should exit if RandomF hits 2 but its not??

Code:
/******************************************************************************/
void InitPre()
{
   EE_INIT();
   App.flag|=APP_MAXIMIZABLE|APP_MINIMIZABLE|APP_RESIZABLE;
  
}
bool Init()
{
   return true;
}
void Shut()
{
}
bool Update()
{
   if(Kb.bp(KB_ESC))return false;
   Gui.update();
    D.clear();
   return true;
}

void Draw()
{  Gui.draw();
    TextStyle ts;
      ts.color=WHITE;
      ts.scale*=0.5;
      
   flt R = RandomF(1, 2); if (R==2) Exit();
      D.text(ts, 0, 0.8, S+"RandomF = "+R);
  

}
/******************************************************************************/
09-30-2013 09:23 PM
Find all posts by this user Quote this message in a reply
Pherael Offline
Silver Supporter

Post: #5
RE: randomF
It's because of precision.
This is totaly normal, and you never should equated floating type.
This is good article why is that http://www.cygnus-software.com/papers/co...floats.htm
or more futher - http://docs.oracle.com/cd/E19957-01/806-...dberg.html

You should remember that float has an approximate value.

Also chance that RandomF give you excatly max value (if using 32-bit randomizer) is 1 to 2^32 what is 1 to 4294967296.

I'm not good at english, so I try to picture it:

if float type would have one number after point - RandomF(1,2) give you back one of this:
1.0 | 1.2 | 1.3 | 1.4 | 1.5 | 1.6 | 1.7 | 1.8 | 1.9 | 2.0
As you see chance to get 2.0 is 1 to 11

if float would have two number after point - RandomF(1,2) give you back:
1.00 | 1.01 | 1.02 ....... | 1.97 | 1.98 | 1.99 | 2.00
Now, chance to get excatly 2.00 is 1 to 101

Now try imagine that with 32-bit float in c++ randomF(1,2) give you something from
1.000000000000000 to 2.000000000000000

I hope it's not mutter to you since I'm not good with explain things.
(This post was last modified: 09-30-2013 10:01 PM by Pherael.)
09-30-2013 09:59 PM
Find all posts by this user Quote this message in a reply
compulsive compiler Offline
Member

Post: #6
RE: randomF
Float myfloats[5];
Float a=0.5f;

For(int i =0;i<5;i++)
{
myfloats[i]+=a;
}

Float randomfloat=floats[random(0,5)];

.......ofcourse half coded example sure
you could figure the rest
(This post was last modified: 09-30-2013 10:36 PM by compulsive compiler.)
09-30-2013 10:31 PM
Find all posts by this user Quote this message in a reply
SamerK Offline
Member

Post: #7
RE: randomF
Thanks Pharael. what you've said was clear.
So, based on my understanding I made a change in the code by putting RandomF(1,2) on a loop for 100000000 times and I could get 2.0, however it failed for shorter loops.
Code:
/******************************************************************************/



void InitPre()
{
   EE_INIT();
   App.flag|=APP_MAXIMIZABLE|APP_MINIMIZABLE|APP_RESIZABLE;
  
  
   FREP(100000000)
   {
        flt R = RandomF(1, 2); if (R==2) Exit("2.0 FOUND ----DONE :)");
   }
  
  
  
  
}
bool Init()
{
   return true;
}
void Shut()
{
}
bool Update()
{
   if(Kb.bp(KB_ESC))return false;
   Gui.update();
    D.clear();
   return true;
}

void Draw()
{
  
   Gui.draw();
    
  
}

But why RandomF couldn't produce 2.0 when it was inside the infinite loop of Draw()
(This post was last modified: 09-30-2013 10:35 PM by SamerK.)
09-30-2013 10:32 PM
Find all posts by this user Quote this message in a reply
para Offline
Member

Post: #8
RE: randomF
because you can't do if (R==2), Pharael was correct, you can't use == with floats and not expect trouble.. you should check if R is in "range", 2.0-epsilon < R < 2.0+epsilon, with epsilon being a very small number..

Code:
FREP(100000000)
{
     flt R = RandomF(1, 2); if( Abs(R - 2.0) < 0.00001 ) Exit("2.0 FOUND ----DONE :)");
}
09-30-2013 10:51 PM
Visit this user's website Find all posts by this user Quote this message in a reply
aceio76 Offline
Silver Supporter

Post: #9
RE: randomF
Esenthel already thought about this and has a built-in method:

// if equal with custom epsilon
Bool Equal(Flt r0, Flt r1, Flt epsilon);
10-01-2013 12:19 AM
Visit this user's website Find all posts by this user Quote this message in a reply
1991mirec Offline
Member

Post: #10
RE: randomF
thank you this helped me a lot... i figured it out.. i had flt number there.. smile
10-12-2013 10:52 AM
Find all posts by this user Quote this message in a reply
Post Reply