View unanswered posts | View active topics It is currently Sat Jun 25, 2016 5:30 pm

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Random() function not working in V3.62
Author Message
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
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 effectresult=random(255); //every value between 0 and 255 works OKresult=random(256); //returns only 0result=random(257); //returns 0 or 1result=random(258); //returns 0 or 1 or 2                            //   and so onint X=5000;result=random(X); //this worksconst int X=5000;#define X 5000;//both have same result as result=random(5000);// rand() function seems to work OKX=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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Re: Random() function not working in V3.62
Quote:
 Code:   Code:X=random(255); //every value between 0 and 255 works OKX=random(256); //returns only 0X=random(257); //returns 0 or 1X=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)

Fri Dec 06, 2013 5:03 pm
Novice

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

Fri Dec 06, 2013 6:19 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Re: Random() function not working in V3.62
 Quote:long result;// int result; // has same effectresult=random(256); //returns only 0result=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 OKX=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)

Sat Dec 07, 2013 5:00 am
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
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 -32768long maxRand=0; //maximum will be 32767long result=0;long cycleCount=0;task main(){   while (true)   {      result=rand();      if(result>maxRand)         maxRand=result;      if(result

so rand() seems to return a signed int

Sat Dec 07, 2013 5:51 am
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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)

Sat Dec 07, 2013 5:53 am
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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)

Sat Dec 07, 2013 5:57 am
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
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

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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 updated help documentation and the ROBOTC Forums.

Mon Dec 09, 2013 6:42 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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 randomizeunsigned 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)

Tue Dec 10, 2013 3:45 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 11 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning