I'm having trouble with this program that is a math tutor for a young student. The program should display two random numbers between 1 and 500. Could I get some help?
I'm having trouble with this program that is a math tutor for a young student. The program should display two random numbers between 1 and 500. Could I get some help?
What have you tried so far?
So far I have:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
unsigned seed;
cout <<"Enter a seed value: ";
cin >>seed;
srand (seed); //Call srand to set a "seed" before
//any random numbers can be generated
cout <<rand() <<endl;
cout <<rand() <<endl;
cout <<rand() <<endl;
return 0;
}
rand() returns a value between 0 and RAND_MAX.
You need to apply some math to bound that value. In C++, the remainder operator is %.
int value = (rand() % 499) +1;
I believe the largest value that your code can generate is 499 :D
>% operator removes randomness quite a bit.
A problem which is statistically insignificant unless you have very specific distribution and period needs (in which case you'd use a much better random number generator) or the range is very small (such as rand() % 2
). For simple programs that just need something "random enough", using remainder to fix the range is fine. More details about what's happening and why (as well as how to fix problems properly) can be found here.
A problem which is statistically insignificant unless you have very specific distribution and period needs (in which case you'd use a much better random number generator) or the range is very small (such as rand() % 2). For simple programs that just need something "random enough", using remainder to fix the range is fine. More details about what's happening and why (as well as how to fix problems properly) can be found here.
Wow, if you take out the waffle that Julienne dude would have a great, informative website.
The waffle adds to its charm. ;)
The waffle adds to its charm.
I find the comic sans font around the circular logo endearing too.
I believe the largest value that your code can generate is 499 :D
Yoinks! Half asleep math again!
Sorry :$
Ok...I am just a student and a noob one at that so fill free to correct me and raze me into the ground so I learn, but last night in my reading and doing an assignment I worked with rand() for the first time. To quote:
% operator removes randomness quite a bit.
My book did say something about this and I believe its answer for fixing that was this: (using the example from before)
int value = ((rand() + time(0)) % 499) +1;
Now I don't have my book right infront of me now as I am suppose to be working, so it is possible my syntax is wrong but hopefully that is enough to give you, the pro's ;), a good enough idea of what I am trying to talk about. Something about the time(0)
helping make it more random. Ok....let the razing begin!
Edit: I corrected a ":(" problem as I did not mean to have a sad face, I am quite happy actually, thanks for asking.
>int value = ((rand() + time(0)) % 499) +1;
Interesting, but not a good idea. First, using a time_t value (the type that time returns) in basically any arithmetic operation besides comparison to (time_t)-1 isn't guaranteed to be meaningful. Second, unless time_t is unsigned (and it may not be), you're risking integer overflow with (rand() + time(0))
. Finally, this won't fix the distribution problem of using remainder to adjust the range of rand. In fact, it's more likely to make it worse. ;)
>Something about the time(0) helping make it more random.
I'd guess that the book meant using time(0) as the argument to srand, which should be called once when the program starts to seed your random number generator:
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
std::srand ( std::time ( 0 ) );
for ( int i = 0; i < 10; i++ )
std::cout<< std::rand() % 10 <<'\n';
}
All that does is changes the sequence between runs of the program. rand is seeded to 1 by default, so if you don't call srand, you'll keep getting the same sequence if you run the program multiple times. That's a different issue from using the remainder operator to fix the range that rand returns, which is an issue of all values in the range no longer being equally probable.
ok.....so I was typing out this long @$$ post on telling you what I was doing for the assignment and how it worked and then I remembered.
I believed I had to use the time(0) to lock the number not make it more random. I was learning how to do while loops for the first time and use a bool to end the loop. I think the (rand() + time(0)) made the loop hold on to the same number until the loop ended. The user was suppose to guess a number between 0 and 100, and have 5 tries to guess the number. So, in that respect, last night running the program multiple times, rand() never once picked the same number. So the only reason I can think of that the % makes it not as random is because you are restricting the pool the computer can pull from. A number btwn 1 & 10 will never be as random as a number btwn 1 & 1000. Right?
>A number btwn 1 & 10 will never be as random as a number btwn 1 & 1000. Right?
Close. rand is designed to return a number between 0 and RAND_MAX, where each number in that range is equally probable. That means every time you call rand, you have a 1 in RAND_MAX chance of getting every number between 0 and RAND_MAX. If you restrict the range with the remainder operator, chances are very good that you'll break that distribution such that some numbers in the new range are more likely to be returned than others. So the sequence isn't as "random" as it would be if you didn't restrict the range.