ROBOTC.net forums http://www.robotc.net/forums/ 

Random Number Generator http://www.robotc.net/forums/viewtopic.php?f=1&t=3884 
Page 1 of 1 
Author:  Darlena830 [ Sun Dec 18, 2011 9:31 pm ] 
Post subject:  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. 
Author:  magicode [ Sun Dec 18, 2011 10:41 pm ] 
Post subject:  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. 
Author:  mightor [ Mon Dec 19, 2011 2:23 am ] 
Post subject:  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 
Author:  Darlena830 [ Mon Dec 19, 2011 3:39 am ] 
Post subject:  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. 
Author:  magicode [ Mon Dec 19, 2011 11:47 am ]  
Post subject:  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:
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:
If you build on that, you can make the numbers which create the standard deviation that you want be more probable. 
Author:  Coder A [ Thu Jan 03, 2013 6:07 pm ] 
Post subject:  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 
Author:  mightor [ Fri Jan 04, 2013 3:10 am ]  
Post subject:  Re: Random Number Generator  
Shouldn't that be:
Anyway, I wrote some code around your bellrandom() function and I noticed something when you graph out the values:
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

Author:  Coder A [ Fri Jan 04, 2013 11:23 am ]  
Post subject:  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:
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.
Once again, thanks for the help. I am always open for it! Coder A 
Author:  RoboDesigners [ Fri Jan 04, 2013 12:19 pm ] 
Post subject:  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 
Author:  mightor [ Fri Jan 04, 2013 12:29 pm ]  
Post subject:  Re: Random Number Generator  
Instead of simply casting a float to int, I used round() in my function.
The curve looks a lot more belllike now
= Xander 
Author:  magicode [ Fri Jan 04, 2013 1:56 pm ] 
Post subject:  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 BoxMuller 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. 
Author:  Coder A [ Fri Jan 04, 2013 3:15 pm ] 
Post subject:  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! 
Page 1 of 1  All times are UTC  5 hours [ DST ] 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ 