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;
Thursday, July 9, 2009
Subscribe to:
Post Comments (Atom)
2 comments:
I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Susan
http://dclottery.info
can someone help me by writing a c++ program for the following problem:
C++ program for share market game. The user starts with a cash of $1,000.00, and each day has the option to: (B)uy, (S)ell, (H)old or (Q)uit options should be error-checked and the user re-prompted if incorrect
There is only one fund traded on this exchange (an index fund), and its starting share price is $1.00. Each day, the price varies randomly up, down or steady. The new share price is the old one multiplied by 0.5, 1.0, 1.5 or 2.0 (with equal/random probability). The user transacts in the morning, the share price changes, then the user is presented with their details for the end of the day:
Buy, (buy amount divided by share price).
Sell, (sell amount divided by share price).
Your program must error-check the buy and sell amounts so that they can’t spend more than they have (or less than $0), and they can’t sell more than they have (or less than $0). Re-prompt the user if their input is incorrect.
If a user chooses to Hold, then the share price is adjusted and accordingly the share account balance.
When the user chooses to Quit, all of their shares are sold at the current price and they receive a final report with the number of days they were in the market, the final balance, the total profit or loss they made and the average profit/loss per day. If they quit on the first day, then this is zero days and they get a farewell message but no report.
Post a Comment