1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help With A Program Using Rand()

  1. Nov 7, 2007 #1
    1. The problem statement, all variables and given/known data
    The question is a dice is rolled to decide a stock's value. So it can either go up or down, and can change in value by .05,.10 and .20. We need to first write a function that will roll a dice and report how much the stock will change by. Then in the main part of the program, we need to roll a dice to see which of the 6 stocks are affected, and then call the function to see how much it is changed by. We are supposed to do this five times and then print out the results of the six stocks(which are in an array).

    3. The attempt at a solution
    My problem is when I print out the array, the values havn't changed. What am I doing wrong. I'll paste my code here, and thanks for helping!

    http://cpp.tastethepaste.org/1469
     
  2. jcsd
  3. Nov 7, 2007 #2

    DaveC426913

    User Avatar
    Gold Member

    Show your data as well - your initial values and your final values.

    Also, pepper your code with couts so you can more easily step through its calcs.

    So far, I see no flaw.
     
    Last edited: Nov 7, 2007
  4. Nov 7, 2007 #3
    The 6 initial values of the stocks are .60,.70,.85,1.0,1.20,1.55, which you can see on line 47, and when they are printed out after the program has run, the values are the same.
     
    Last edited: Nov 7, 2007
  5. Nov 7, 2007 #4

    DaveC426913

    User Avatar
    Gold Member

    Put some couts in there.

    Is it, in fact, hitting any of those randNum tests in the count loop? Is it hitting the randNumOne and the randNumTwo tests in the changeInValue loop?


    BTW, meaningful variable names go a long way toward debugging - your randNumOne/Two variables are hard to sort out.
     
    Last edited: Nov 7, 2007
  6. Nov 7, 2007 #5
    Well, from what I have been reading, the range of the rand function works like this: rand() % n where the range is 0 to n-1. So if I go rand() %3, the range should be 0 to 2 from what I understand. Am I out to lunch. And also, I have the cout at line 79. Again, thank you for your help and patience! And sorry about the poor choice of variable names.
     
  7. Nov 7, 2007 #6

    DaveC426913

    User Avatar
    Gold Member

    That looks right.

    No, I mean put more throughout your code, so you can see the values as (or if) they change. Somehere in your code, values are not getting set where you think they are.
     
  8. Nov 8, 2007 #7
    Ok, here is the problem. For instance, In the while loop at the end, I assign the variable randNum the rand function, but during the 5 times the while loop runs, randNum keeps the same value it got the first time it ran. So if on the first time rand() game me 5, all five time through the loop randNum is 5. I'm not sure how to fix this. The same goes for my change variable.
     
    Last edited: Nov 8, 2007
  9. Nov 8, 2007 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    What's wrong:

    1. You are calling srand WAY too often. You should call it one time in the program, and never call it again. This is almost certainly the problem that bit you. The rest of the mistakes will bite you in the future if you repeat making them.

    What bit you is that each of those repeated calls to srand(time(0)) reset the random number generator, and reset it to exactly the same sequence since your program almost certainly runs in less than one second.

    2. You are using the least significant bits of rand, but that is probably what you were told to use. The least significant bit (rand()%2) is not random in many implementations of the function. One more than one machine, it goes 0,1,0,1,0,1 on successive calls to rand(). The high-order bits are much more reliably "random". Recommended practice: convert the number to a double via (double)rand()/((double)RAND_MAX). Now you can check for high versus low by comparing to 0.5, etc.

    3. You are using srand/rand, but that is probably what you were told to use.

    [ soapbox ]
    srand and rand are part of the C and C++ standards, with more-or-less the same specs the two standards. There have been several notoriously bad random number generators that satisfy the specs. The C/C++ standards is so downright lousy in this regard that most professional analysts never use srand/rand. Never.
    [ /soapbox ]
     
  10. Nov 8, 2007 #9

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    DH is right. If you must use srand/rand, then call srand once, possibly in main().
    srand seeds the random number generator. If you reseed it with the same value then you get the same sequence of numbers each time.

    srand/rand is based on a PRNG Don Knuth presented in a 3 volumes series, 'The Art of Computer Programming' from 1968. A lot of what is in the C standard is there to support legacy code, stuff written 20 years ago that is still in production. rand() is one of those.
     
  11. Nov 8, 2007 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Don't blame Knuth. Most botched random number generators use a linear congruent generator, which is quite distinct from Knuth's subtractive method.

    Any pseudo random number generator will eventually start repeating itself if called enough times. Think of a pseudo random number generator as if it has a large (preferrably, a very very large) precomputed sequence of numbers such that a small subsequence appears to be "random". rand() is equivalent to returning the next number in the sequence while srand() changes the current location in the sequence. A good generator cannot actually precompute its sequence, but the model is a very good one nonetheless. (For a good generator the sequence length may exceed 1048 numbers. That much storage does not exist.)

    With that model, some bad implementations of srand/rand have a sequence length of 32768. Don't blame that on Knuth. This is the fault of a lousy implementation of a lousy standard.
     
  12. Nov 8, 2007 #11
    I got rid of the second srand I had in there, leaving only the one in the function I wrote at the top. Now, when I call a random number between 0 and 5 at the bottom, the first time through the loop it will pick a different number, but for the 4 more times it executes, it is the same number. So, only 2 stocks are affected. Also, I got rid of the change variable at the bottom and instead of adding the change, I just add the function changeInValue(), but the value is the same all 5 times I run it. Now im really confused...
     
  13. Nov 8, 2007 #12

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You got rid of the wrong srand. You should call srand once per program execution, not once per function call.
     
  14. Nov 8, 2007 #13
    Thank you so much for your help and patience, my program now works!! Thank you everyone!!
     
  15. Nov 8, 2007 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You're welcome, and thanks for the thanks. We homework helpers do appreciate the kudos.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Help With A Program Using Rand()
  1. Programming Help (Replies: 4)

Loading...