View unanswered posts | View active topics It is currently Tue Sep 23, 2014 11:15 am






Reply to topic  [ 11 posts ] 
Random() function not working in V3.62 
Author Message
Rookie

Joined: Sat Apr 12, 2008 11:09 am
Posts: 49
Location: holland
Post Random() function not working in V3.62
Since i've upgraded to RobotC for mindstorms V 3.62 the random() function seem to work different
Code:

long result;
// int result; // has same effect

result=random(255); //every value between 0 and 255 works OK
result=random(256); //returns only 0
result=random(257); //returns 0 or 1
result=random(258); //returns 0 or 1 or 2
                            //   and so on

int X=5000;
result=random(X); //this works

const int X=5000;
#define X 5000;
//both have same result as result=random(5000);

// rand() function seems to work OK
X=rand() %10000+10000; //returns values between 0 and 19999 as it should


It seems that the value between the brackets it trunctuated to the first byte
Has the function changed or am i doing something wrong?


Last edited by nxt on Fri Dec 06, 2013 6:18 pm, edited 1 time in total.



Fri Dec 06, 2013 4:59 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Random() function not working in V3.62
Quote:
Code:
   
Code:

X=random(255); //every value between 0 and 255 works OK
X=random(256); //returns only 0
X=random(257); //returns 0 or 1
X=random(258); //returns 0 or 1 or 2
                            //   and so on

what is your very 1st declaration of X ?
I don't see if it's char or unsigned char or int or unsigned int or long or unsigned long...? Or maybe even float...?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 06, 2013 5:03 pm
Profile
Rookie

Joined: Sat Apr 12, 2008 11:09 am
Posts: 49
Location: holland
Post Re: Random() function not working in V3.62
Sorry messed things up a little. Changed the code


Fri Dec 06, 2013 6:19 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Random() function not working in V3.62
if your reported mistake
Quote:
long result;
// int result; // has same effect

result=random(256); //returns only 0
result=random(257); //returns 0 or 1

now persists then it's surely a RobotC bug

I'm also not sure about the rand() function.
in C rand() should return a value >=0 and < RAND_MAX which is at least 32767 on any standard library implementation (would you pls check this point?).
So in your example the result
Quote:
// rand() function seems to work OK
X=rand() %10000+10000; //returns values between 0 and 19999

is supposed to be faulty, it should give a value between >=10000 and <=19999 instead.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Dec 07, 2013 5:00 am
Profile
Rookie

Joined: Sat Apr 12, 2008 11:09 am
Posts: 49
Location: holland
Post Re: Random() function not working in V3.62
I am not a C programmer so had to look it up.
You are right about the rand() function.
Rand() should indeed only return a number >=0.

i tested the following code for about 10 million cycles
rand returns numbers between -32768 and 32767

Code:
long minRand=0; //minimum will be -32768
long maxRand=0; //maximum will be 32767
long result=0;
long cycleCount=0;
task main()
{
   while (true)
   {
      result=rand();
      if(result>maxRand)
         maxRand=result;
      if(result<minRand)
         minRand=result;
      ++cyclecount;
   }
}


so rand() seems to return a signed int


Sat Dec 07, 2013 5:51 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Random() function not working in V3.62
then this is supposed to be another bug IMO.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Dec 07, 2013 5:53 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Random() function not working in V3.62
so I would suggest you to report these bugs in the bug section.

now there are 2 ways to go:
- either you'll wait until Robomatter will have fixed these issues
- or I can provide you with a code which features a quite good pseudo random number generator (PRNG, a linear congruence generator = LCG by K&R ) which works very reliable and highly equally distributed even over subsets of modulo.

(I also got an extremely high-level PRNG based on a Mersenne Twister, but this code possibly is beyond the scope of what is necessary.)

It would be helpful if you could check and display the value of
RAND_MAX
to see how it already has been implemented by RC and to what value it will be expanded (probably 2.147.483.647 (max_signed_long) or even bigger (max_unsigned_long)).

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Dec 07, 2013 5:57 am
Profile
Rookie

Joined: Sat Apr 12, 2008 11:09 am
Posts: 49
Location: holland
Post Re: Random() function not working in V3.62
i can not find the RAND_MAX value.

the following code:
Code:
long X=5000;
result=random(X);

does work. It returns values from 0 <=5000
I will use that until it is solved.
I use it just for testing the accuracy and repeatabillity of a delta robot, so real randomness or advanced functions is not important.
I am just trying to use less variables.


Sat Dec 07, 2013 6:31 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Random() function not working in V3.62
Hi there,

Please send a mail to support@robotc.net so it'll get logged :)

Thanks!

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Dec 07, 2013 7:28 am
Profile WWW
Online
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 578
Post Re: Random() function not working in V3.62
Thanks for the heads up on this one. The random and rand functions have a known issue that we are working on; in the meantime, you can use randlong in the meantime, but you will need to make one modification to the RobotCIntrinsics.c file (typically found in the C:/Program Files (x86)/Robomatter Inc/ROBOTC Development Environment/Includes directory). You will need to remove the #if and #endif lines from the following segment of code:

Code:
#if defined(UseLongs)
intrinsic long randlong(void)  asm(opcdSystemFunctions, byte(sysFuncRandomLong), functionReturn);
intrinsic void srand(const long nSeedValue) asm(opcdSystemFunctions, byte(sysFuncSRandLong),  variableRefLong(nSeedValue));
#endif


Which becomes:

Code:
intrinsic long randlong(void)  asm(opcdSystemFunctions, byte(sysFuncRandomLong), functionReturn);
intrinsic void srand(const long nSeedValue) asm(opcdSystemFunctions, byte(sysFuncSRandLong),  variableRefLong(nSeedValue));


This fix will allow you to use randlong/srand in lieu of rand/random.

Thank you again for bringing this to our attention!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Dec 09, 2013 6:42 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Random() function not working in V3.62
John,
IMO it's more than easy to fix this issue - just use this following code instead
(rename randK to rand and srandk to srand and randomizeK to randomize).

Be sure to have #defined RAND_MAX correctly in your math libs like, e.g.
#define RAND_MAX 2147483647 // == LONG_MAX

the line
seed = abs(CurrentTick()*BatteryLevel());} // substitute to time(0)
has to be adjusted to RobotC syntax of course, just like the rest of my NXC code:

Code:
//-------------------------------------------------------
// Kernighan & Ritchie LCG (linear congruence generator)
//-------------------------------------------------------
//
// how to use:
// srandK(x)         // call once at the start to seed the PRNG:
                     // x = const. for repetitive sequences;
                     // x = 0 generates even a "randomized random" seed
// unsigned long myVar = randK()         // generates a PRN between 0 and RAND_MAX-1
// unsigned long myVar = randK() % 100;  // assignes a random number between >=0 and <100
// unsigned long myVar = randomK(100)    // == alternatively
//
//-------------------------------------------------------
//
// global variables and functions to plant a random seed and to randomize

unsigned long _RAND_SEED_ = 1;      // 1= default value (program start)
unsigned long _OLD_SEED_  = 1;


//--------------------------------------------

inline unsigned long randK()               // custom random function
{
  _RAND_SEED_ = _RAND_SEED_ * 1103515245 + 12345;
  return (_RAND_SEED_ % (RAND_MAX + 1));
}

//--------------------------------------------


inline void srandK(unsigned long seed)      // seeds for a new random series
{

  if (seed==0)                      // 0: a "real" randomized random seed
    {seed = abs(CurrentTick()*BatteryLevel());}  // substitute to time(0)

  else
  if (seed==-1) {                   // -1: restore last random series
    seed = _OLD_SEED_;
  }

  _OLD_SEED_  = seed;
  _RAND_SEED_ = seed;               // patch for randK_ function
}

//--------------------------------------------

unsigned long randomK(unsigned long N)   {
  return (randK()%N);
}



HTH!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Tue Dec 10, 2013 3:45 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: JohnWatson, t1techno and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.