Help With A Program Using Rand()

  • Thread starter NDiggity
  • Start date
  • Tags
    Program
In summary: It's what you're supposed to use.In summary, you are trying to use the rand function but are doing it incorrectly. You need to call it once and never call it again, and use the high-order bits of rand. Also, you are using the least significant bits of rand, but that is probably what you were told to use.
  • #1
NDiggity
54
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
  • #2
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:
  • #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:
  • #4
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:
  • #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.
 
  • #6
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 cout 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.
 
  • #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:
  • #8
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 ]
 
  • #9
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.
 

1. What is the purpose of using Rand() in a program?

The Rand() function is used to generate random numbers in a program. This can be useful in situations where you need a random value, such as in a game or simulation.

2. How does the Rand() function work?

The Rand() function uses an algorithm to generate a random number between 0 and 1. This number is then multiplied by the range of numbers you specify, and then rounded to an integer if necessary. This process is repeated every time the function is called, resulting in a different random number each time.

3. Can I specify a range for the numbers generated by Rand()?

Yes, you can specify a range for the numbers generated by Rand(). This is done by using the modulus operator (%), which will give you a random number within the specified range. For example, if you want a random number between 1 and 10, you can use Rand() % 10 + 1.

4. Are the numbers generated by Rand() truly random?

No, the numbers generated by Rand() are not truly random. They are generated using an algorithm, which means they are predictable to some extent. However, for most purposes, the numbers generated by Rand() are random enough.

5. Are there any other functions similar to Rand()?

Yes, there are other functions similar to Rand(). Some examples include RandRange(), Random(), and Randomize(). These functions may have slightly different syntax or capabilities, but they all serve the same purpose of generating random numbers in a program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
896
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
897
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
Back
Top