Sunday, May 10, 2009

Please interpret c++ code; random number generator?

I am trying to generate a random number specifically in the range (-1, 1). I want to understand what the difference between the two lines below is; the first one gives me what I am looking for but i don't quite understand how it works. the second line does not work at all and I was wondering if anyone could suggest any alternatives. Thank you.





1. u = (2.0*rand()/RAND_MAX) - 1;


2. u = rand() / RAND_MAX ;

Please interpret c++ code; random number generator?
1. u = (2.0*rand()/RAND_MAX) - 1;





rand()/RAND_MAX





This gives you a number between [0,1]


Then you multiply by 2 and subtract 1.





You see:


2 * 1 = 2 , 2 -1 = 1 your max


2 * 0 = 0 , 0 -1 = -1 your min





2. u = rand() / RAND_MAX ;


This one only gives you values from 0 to 1
Reply:Some compilers could do rand()%3 - 2 but it's not guaranteed safe.





The following URL goes into a bit more detail: http://www.daniweb.com/forums/thread1769...
Reply:Hi,





Lets first look at the definition of the function rand():


int rand ( void );


Returns a pseudo-random integer number A in the range 0 to RAND_MAX or formally in the interval [0...RAND_MAX], A iselementof [0...RAND_MAX].





Lets first start with the second line of code, as this is the simplest one. The line u = rand() / RAND_MAX actually scales the generated number A=rand() so that it lies in the interval [0...1]. This can be seen as follows:


If A is the smallest number that can be generated by rand(), that is 0 then you have 0 / RAND_MAX = 0.


Likewise, if A is the largest number that can be generated by rand(), that is RAND_MAX then you have RAND_MAX / RAND_MAX = 1. In other words u iselementof [0/RAND_MAX...RAND_MAX/RAND_MAX] = [0...1].





The second line of code:


u = (2.0*rand()/RAND_MAX) - 1;





rand()/RAND_MAX generates a random number in the interval [0...1].





2*rand()/RAND_MAX generates a random number in the interval 2*[0...1] = [0...2].





2.0*rand()/RAND_MAX) - 1 generates a random number in the interval 2*[0...1] - 1 = [0...2] - 1 = [-1...1].
Reply:I think to answer your question then you have to understand what rand() is doing. The rand() function is returning a value between 0 and 1. This value can be multiplied against another value to give a desired random value within a range between 0 and that number. Why this works is beecause the decimal number you get out of rand(), when multiplied against a nubmer, is giving you a percentage of that number.





Remember that 100% is equal to 1 in decimal form. 50% is equal to .5 in decimal form. So if you want a random number between 0 and 100 and you multiple this percentage against it you will get a random number in that range. 100 * 1 = 100 ... 100 * .5 = 50





So with that in mind, now you should be able to understand what 2 * rand() is doing ... it gives you a random number between 2 and 0.





RAND_MAX is equal to 1, so that is doing nothing for you at all. You can forget about the divide by RAND_MAX. A number divided by 1 is itself.





The -1 is the key to getting the value in the range you want. if you take a random number between 0 and 2 and subtract 1 then you now have a random number between -1 and 1. (0 - 1 = -1 ... 2 - 1 = 1)





So really, the only thing you need is just the 2.0 * rand() - 1;


No comments:

Post a Comment