Help With A Program Using Rand()

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to simulating stock value changes based on dice rolls using the rand() function in C++. Participants are addressing issues with random number generation, debugging techniques, and the correct implementation of the program logic.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a homework problem involving rolling a dice to determine stock value changes and expresses confusion about why the stock values remain unchanged after execution.
  • Another participant suggests showing initial and final stock values and using debugging outputs (cout) to trace calculations.
  • Participants discuss the range of the rand() function and whether the implementation of random number generation is correct, with one participant questioning their understanding of rand() % n.
  • Concerns are raised about calling srand too frequently, with a participant noting that repeated calls reset the random number generator, leading to the same sequence of numbers being generated.
  • Some participants suggest using higher-order bits of rand for better randomness and criticize the standard rand() implementation as inadequate for serious applications.
  • One participant reports that after removing a second srand call, the program still produces the same random number for multiple iterations, indicating a potential misunderstanding of how to implement randomness correctly.
  • Another participant emphasizes the importance of calling srand only once per program execution to avoid resetting the random number sequence.
  • A participant expresses gratitude for the assistance received, indicating that their program is now functioning correctly.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correctly implementing srand and rand for random number generation, but there are differing opinions on the effectiveness and reliability of the standard rand() function. The discussion includes multiple viewpoints on debugging strategies and the implications of using certain coding practices.

Contextual Notes

Participants mention limitations related to the randomness of the rand() function and the potential for repeated sequences due to improper use of srand. There are unresolved questions about the best practices for random number generation in C++.

NDiggity
Messages
53
Reaction score
0

Homework Statement


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).

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
 
Physics news on Phys.org
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:
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:
NDiggity said:
The 6 initial values of the stocks are .60,.70,.85,1.0,1.20,1.55 and when they are printed out after the program has run, the values are the same.
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:
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 count at line 79. Again, thank you for your help and patience! And sorry about the poor choice of variable names.
 
NDiggity said:
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.
That looks right.

NDiggity said:
And also, I have the count at line 79.
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.
 
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:
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 ]
 
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.
 
  • #10
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.
 
  • #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 I am really confused...
 
  • #12
You got rid of the wrong srand. You should call srand once per program execution, not once per function call.
 
  • #13
Thank you so much for your help and patience, my program now works! Thank you everyone!
 
  • #14
You're welcome, and thanks for the thanks. We homework helpers do appreciate the kudos.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K