View unanswered posts | View active topics It is currently Wed Jun 29, 2016 3:32 am

 Page 1 of 1 [ 12 posts ]
 Print view Previous topic | Next topic
Random Number Generator
Author Message
Rookie

Joined: Sun Dec 18, 2011 8:41 pm
Posts: 2
Random Number Generator
Does anyone know if a random number generator can retrieve numbers with a standard deviation from a set number instead of a statistical equal distribution? We're using an NXT brick, and rather new to programming. If someone could help us out we'd really apprecaite it.

Sun Dec 18, 2011 9:31 pm
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: Random Number Generator
So you want to pick a number, say 20, and have the numbers be within 6 of 20? Or do you want to standard deviation to be 6 from 20, in which case you could have say 27 and 13.

_________________
sudo rm -rf /

Sun Dec 18, 2011 10:41 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Random Number Generator
Moved to a better sub forum. Also please don't cross post to multiple sub forums, I deleted the duplicate one.

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

Mon Dec 19, 2011 2:23 am
Rookie

Joined: Sun Dec 18, 2011 8:41 pm
Posts: 2
Re: Random Number Generator
We would like a standard deviation to exist, the possibility of a large sequence of numbers to be retrieved, but the probability of number x being retrieved to increase as it approaches a programmer defined arithmetic mean value, such that, if the number were to be plotted against its probability of being retrieved on a Cartesian plane, a bell curve would emerge.

Mon Dec 19, 2011 3:39 am
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: Random Number Generator
Well, here's one way that you could go about going this: Instead of having the random number generator pick out the number that you are going to use directly, use the random number generated to statistically pick your own number. Write a function that returns a number based on the range in which the random number generated. If you were picking random numbers from 1 - 10, and wanted each number to have an equal chance of appearing, you would say:
 Code:if the random number generated is from (0 to (random number range / 10)){  return 1;if the random number generated is from ((random number range / 10) to 2*(random number range / 10)){  return 2;

ect. (of course, you would write a formula so you don't have to do this many calculations, but this is just an example)

If you had the same scenario, but wanted the number 3 to be more probable than the others, then you would increase the range in which the number 3 is returned:
 Code:if the random number generated is from (0 to (random number range / 11)){  return 1;if the random number generated is from ((random number range / 11) to 2*(random number range / 11)){  return 2;if the random number generated is from (2*(random number range / 11) to 4*(random number range / 11)){  return 3;

If you build on that, you can make the numbers which create the standard deviation that you want be more probable.

_________________
sudo rm -rf /

Mon Dec 19, 2011 11:47 am
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 207
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Re: Random Number Generator
I have been looking for a challenge and a way to help a fellow programmer, and here have I found it! What you are looking for is something called a random walk, in which a number of "steps" are taken in one of two directions. In your case, these steps are taken from your set number, each step's length is 1/2, and twice as many steps are taken as the number which is the deviation from the set number. Such a function would have one input and would look like this:

int bellrandom(short deviation)
{
int ReturnNum = 0;
for(int i = 0;i < deviation*2;i++)
{
ReturnNum = ((rand()%2)-.5); //adds or subtracts 1/2 to ReturnNum
}
return ReturnNum;
}

If you plotted the return frequency of every number, you would get a perfect bell curve. Notice I did not make the set number an argument passed to the function. To do that, you would do something like this:

To get a random number within 20 of 40:

bellrandom(20) + 40

within 20 of -40:

bellrandom(20) - 40

within 10 of 100, in 2 unit steps:

(bellrandom(5)*2)/*1/2 of 10*/ +100

You get the point. Feel free to use this code for your own use, giving credit when due, but don't patent it and get rich or something!

Post back if this works.

I hope this helps!

-Coder A

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot

Last edited by Coder A on Fri Jan 04, 2013 11:46 am, edited 1 time in total.

Thu Jan 03, 2013 6:07 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Random Number Generator
Shouldn't that be:
 Code:int bellrandom(short deviation){  int ReturnNum = 0;  for(int i = 0;i < deviation*2;i++)  {    ReturnNum += ((rand()%2)-.5); //adds or subtracts 1/2 to ReturnNum  }  return ReturnNum;}

Anyway, I wrote some code around your bellrandom() function and I noticed something when you graph out the values:
 Attachment: 2013-01-04_08-08-46.png [ 5.07 KiB | Viewed 8779 times ]

As you can see, the values drop off dramatically after the peak, much more so than on the other side.
I've included the code below so you can play with it yourself.

Regards,
Xander

 Code:int frequency[100];int bellrandom(short deviation){  int ReturnNum = 0;  for(int i = 0;i < deviation*2;i++)  {    ReturnNum += ((rand()%2)-.5); //adds or subtracts 1/2 to ReturnNum  }  return ReturnNum;}task main(){  memset(frequency, 0, sizeof(frequency));  for (int i = 0; i < 600; i++)  {    frequency[bellrandom(50) + 50]++;  }  for (int i = 0; i < 99; i++)  {    nxtDrawLine(i, frequency[i], i+1, frequency[i+1]);  }  while(true);}

_________________
| 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]

Fri Jan 04, 2013 3:10 am
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 207
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Re: Random Number Generator
Xander:

Yesterday I actually did the same thing and noticed similar errors in my code. Thank you for giving your help. I changed 'ReturnNum' to a float because it is added to in increments of .5 and so it always returned 0 or 1 (rounded to wholes). Anyway, here is my improved but still imperfect code:

 Code:int bellrandom(short deviation){  float ReturnNum = 0;  for(short i = 0;i < deviation*2;i++)  {    ReturnNum += (random(1)-.5); //adds or subtracts 1/2 to ReturnNum  }  return ReturnNum;}

I don't know why the distribution of the numbers would be so poor. My current best solutions are to set 'srand' to 'nsystime',but something else must be going on. Try setting 'ReturnNum' to float type and change '(rand()%2)-.5' to 'random(1)-.5'. Here is the code I used to test out the curve, but because of the low amount of tests made (64) it is not a good way to see what is wrong with your code. However, It seemed to me that there was no dropoff.

 Code:byte statsof[7] = {0,0,0,0,0,0,0};byte tests;//------------------------------------------------------------------------------------------------------------------------//int bellrandom(short deviation){  float ReturnNum = 0;  for(short i = 0;i < deviation*2;i++)  {    ReturnNum = ReturnNum + (random(1)-.5); //adds or subtracts 1/2 to ReturnNum  }  return ReturnNum;}//------------------------------------------------------------------------------------------------------------------------//void displaystats(){  for(byte i=0;i<7;i++)  {    nxtFillRect((i*7)+5,statsof[i]*2,(i*7)+12,0);  }  nxtDisplayStringAt(64,50,"%d",tests+1);}//------------------------------------------------------------------------------------------------------------------------//task main(){  srand(nSysTime);  for(tests=0;tests<64;tests++)  {    statsof[bellrandom(3)+3]++;    displaystats();    wait1Msec(31);  }  while(nNxtButtonPressed == -1){}}

Once again, thanks for the help. I am always open for it!

-Coder A

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot

Fri Jan 04, 2013 11:23 am
Novice

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Re: Random Number Generator
Could someone post a file containing a list of the random numbers that were generated (rather than the graph on the screen)? I've got to fix a problem with my ROBOTC for Mindstorms install... :\

//Andrew

_________________
Check out my website! www.RoboDesigners.com

VRC Team 2190

Fri Jan 04, 2013 12:19 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Random Number Generator
Instead of simply casting a float to int, I used round() in my function.

 Code:int bellrandom(short deviation){  float ReturnNum = 0;  for(int i = 0; i < deviation*2; i++)  {    ReturnNum += random(1)- 0.5; //adds or subtracts 1/2 to ReturnNum  }  return round(ReturnNum);}

The curve looks a lot more bell-like now
 Attachment: 2013-01-04_17-19-07.png [ 5.28 KiB | Viewed 8762 times ]

= 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]

Fri Jan 04, 2013 12:29 pm
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: Random Number Generator
I had done this in C a while ago for a stats class. A relatively simple way to generate a normal distribution is with the Box-Muller Method. I apologize for not having any examples on hand, but you can try it out and see if you get a nice bell curve.

_________________
sudo rm -rf /

Fri Jan 04, 2013 1:56 pm
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 207
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Re: Random Number Generator
The other type of bell curve is one with two halves and no highest probable number, but instead two highest probable numbers. I'll be working on that I don't think the round is necessary because the function adds/subtracts an even number of .5's and any even number times .5 is a whole number. I'm glad the asymmetry problem is fixed!

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot

Fri Jan 04, 2013 3:15 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 12 posts ]

Who is online

Users browsing this forum: jorjatfarika 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