-
SRAND Number Generation Help
I am spending too much time wrapping my head around srand() without success. I need some help around this simple little bit. Here is my timely dilemma...
These are my includes:
#include <cstdlib> //For srand() and rand().
#include <ctime> //For time().
My initializations:
int gameTime = 1200;
double randomTime;
srand(time(0));
randomTime = (rand() % 1000) + 1;
My (supposedly working) generator:
gameTime = randomTime - gameTime;
In the body:
cout << "Do not forget, your current time is: " << gameTime << " minutes.\n";
Dilemma:
The output keeps going back and forth between these two numbers:
Do not forget, your current time is: -660 minutes.
Do not forget, your current time is: 1200 minutes.
And back to 1200 again.
It is supposed to be going from 1200 down to 0, randomly subtracting numbers.
Thank you for any help with this bit. I have been meaning to ask this for months, just been busy.
-
Re: SRAND Number Generation Help
In despite of the fact that your code makes absolutely no sense, what it is lacking is a good way to "seed the randomizer" before it's used. The way it should be done is something like this:
Code:
UINT uSeed = (UINT)time(NULL);
srand(uSeed);
for(int i = 0; i < (uSeed & 0xfff); i++)
rand();
The code above should be called only once when your app starts. This way it will ensure a totally (pseudo-) random number when you call rand().
As for the reasons why you were getting such a non-random result, you must understand that rand() is not technically producing a random number. It simply does the following mathematical function to produce an non-repeating sequence of numbers:
Code:
return ((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff;
The actual "randomness" should be added in the call to srand(), what the code snippet on top attempts to simulate.
-
Re: SRAND Number Generation Help
The code does not make sense because I just gave the main points of my issue. No reason to send over the whole code, is there?
I will see if I can figure out this seeding process. Thanks for the tip.
-
Re: SRAND Number Generation Help
From the little fragment I see, you are only calling rand() once anyway. What do you expect?
Just think for a second about the math. Say your (cough) 'RandomTime' comes out to be 500 the single time you do anything to it.
Pass 1: 500 - 1200 = -700
Pass 2: 500 - (-700) = 1200
Pass 3: 500 - 1200 = -700.
See a pattern there?
-
Re: SRAND Number Generation Help
But when the other commands in main are using the same formula, gameTime should be taking the value from when it last was, so the countdown should keep going down.
I do not know if you people are helping and being funny or criticizing. If you want the whole code, say so. Do not act like this please.
The way I have the code set up, it is supposed to keep looping using the new gameTime each time in each pass and hit 0 eventually.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by TruthSpeaker
If you want the whole code, say so.
Post the smallest and simplest compilable program that demonstrates the problem (i.e., only post your whole code if it does not contain anything irrelevant to this problem.
-
Re: SRAND Number Generation Help
You need to show enough code to reproduce the problem.
Perhaps in this statemen
gameTime = randomTime - gameTime;
you have randomTime and gameTime backwards.
If gameTime starts at 1200 and randomTime is less than 1,000 you'll end up with a negative number at your first subtraction. If that isn't it, show more code.
-
Re: SRAND Number Generation Help
laser, GCD, thank you (no criticism, love it).
I still need to look into DC's example and then if that fails, look into yours and post only the part you need to see.
Thank you.
-
Re: SRAND Number Generation Help
GCDEF, genius! So simple! I swear I have tried that dozens of times before, but may have done so with different variants of generators. Probably did not try with this current method I am using. WOW. See, it was that simple. Thank you.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
TruthSpeaker
Quote:
Originally Posted by GCDEF
Perhaps in this statemen
gameTime = randomTime - gameTime;
you have randomTime and gameTime backwards.
If gameTime starts at 1200 and randomTime is less than 1,000 you'll end up with a negative number at your first subtraction.
GCDEF, genius! So simple! I swear I have tried that dozens of times before, but may have done so with different variants of generators...
:) TruthSpeaker, your common sense is not with you, is it?
Even if you catch that your random number is in the range from 1 to 1000 and you're subtracting 1200 from it, it will still not change the point that you're not sufficiently seeding your randomizer (that was the subject of your question by the way.)
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by dc_2000
it will still not change the point that you're not sufficiently seeding your randomizer (that was the subject of your question by the way.)
I do not think that we can definitely draw that conclusion from TruthSpeaker's original post due to the lack of sufficient context. If the relevant code snippet was from a function that was called many times then that would apply, but it might also be the case that that control passed through that code snippet no more than once.
Your suggested method of seeding the PRNG is interesting though: the actual call to srand() is effectively the same as what TruthSpeaker used, but the difference is that the first few numbers in the sequence are discarded, with the exact number of numbers depending on the seed value. I suspect that that is unnecessary, since if one is able to determine the seed and knows the algorithm, one can still obtain the seed. This article on using rand() proposes a hash of the bytes returned by time(0) as a seed instead, or perhaps just discarding the first number in the sequence.
Quote:
Originally Posted by dc_2000
It simply does the following mathematical function to produce an non-repeating sequence of numbers:
The implementation of rand() is implementation defined and the sequence is not guaranteed to be non-repeating.
-
Re: SRAND Number Generation Help
Quote:
The implementation of rand() is implementation defined and the sequence is not guaranteed to be non-repeating.
Last time I heard, every pseudo random generator is guaranteed to repeat at one point or another. It just depends on how much time it'll take.
rand() usually repeats every 2^32 or 2^64, depending on implementation. What's more problematic, is that if the rand() function uses a linear congruential generator (which it usually does), then it always repeats the same sequence. srand would only define where on that sequence you start.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
laserlight
The implementation of rand() is implementation defined and the sequence is not guaranteed to be non-repeating.
OK, you're right. What I should've said is that the rand() is a simple mathematical function with a long periodicity.
Quote:
Originally Posted by laserlight
Your suggested method of seeding the PRNG is interesting though: the actual call to srand() is effectively the same as what TruthSpeaker used
Again, the reason I suggested that method of seeding is because the OP stated that the output repeats itself, which it may very well do if srand() and rand() are used like he did. The reason? If you look at his code, that effectively becomes this:
Code:
time_t t = time(0);
int holdrand = t;
randomTime = ((((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff) % 1000) + 1;
In which case time() returns the current time in seconds elapsed since midnight of January 1, 1970, then rand() mutiplies it by 214,013 and right-shifts it 16 bits, which is the same as dividing it by 65,536. It then lops off 17 high bits and returns the result. So in a sense it is as if you (roughly) mutiply the time value by 3.3 and remove all the high bits off of the result. As you can imagine the periodicity of that function will be very low. It will also result in almost equal, or repeating results if the OP tried his code one time after another, not waiting for more than a few seconds (since again time() function is involved). What I suggested is a more complex way to seed the randomizer that will eliminate such dependency on "direct" time() call when used in his code.
-
Re: SRAND Number Generation Help
dc, if your rude comments are going to continue, please do not post. I do not know if you trying to be funny but I see no humor. I came here looking for helpful comments, not criticism. go elsewhere for that please.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
TruthSpeaker
I came here looking for helpful comments, not criticism.
Criticism is usually the most helpful kind of comment, so long as it's well thought out.
dc's comments are probably more theoretical than you really need, but you would do well to try to understand what he's saying.
The most important aspect of it? Call srand() only once per program run. Call rand() many times.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
TruthSpeaker
But when the other commands in main are using the same formula, gameTime should be taking the value from when it last was, so the countdown should keep going down.
I do not know if you people are helping and being funny or criticizing. If you want the whole code, say so. Do not act like this please.
The way I have the code set up, it is supposed to keep looping using the new gameTime each time in each pass and hit 0 eventually.
Well, from what you posted, I don't see additional calls to rand(), which you may not want anyway, but I demonstrated how with a single call, and a single number, exactly WHY you were alternating between two discrete values.
I can only base a guess on what you posted. I just followed the math, assuming your:
gameTime = randomTime - gameTime;
statement is what is being executed in your loop.
If you consider that criticism, fine.
-
Re: SRAND Number Generation Help
It is just that there were 2-3 comments that were uncalled for. Not the examples or the help the person is offering, but the way the person presented them. I forgive the person if it is his/her personality and just habit, and I always welcome help, but when I see wording that looks uncalled for, I state so. Am sure it is because of my lack of humor as well. If all is good between us parties, then all is good.
I am going to soon try everyone's suggestions so I can get this simple little bit to generate correct.
Thank you.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
TruthSpeaker
dc, if your rude comments are going to continue, please do not post. I do not know if you trying to be funny but I see no humor. I came here looking for helpful comments, not criticism. go elsewhere for that please.
You will get no end of offensive people being rude to you - they come on this board to pontificate, not to be helpful.
By the way, I'd use Mersenne Twister, rather than the standard library for random generation.
See wikipedia for a downlink for MT.
Code:
init_genrand((unsigned)time(0));
unsigned long genrand_int32(void);
int random_number(int range)
{
if (range < 1) return 0;
return(genrand_int32()%range)+1;
}
-
Re: SRAND Number Generation Help
Hmmm... I am really having a hard time finding any "rude" comments and in fact find the over-sensitivity annoying. Maybe you should try and understand what is being said instead of getting your feelings hurt.
C++ now supports more sophisticated random number generators, Mersenne twister being one of them. So stop fooling around with rand()
http://www.johndcook.com/cpp_TR1_random.html
http://www.boost.org/doc/libs/1_40_0...dom/index.html
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
mwoods
You will get no end of offensive people being rude to you - they come on this board to pontificate, not to be helpful.
You've been a member for two months and have 22 posts to your name. I don't think you really have the experience to make that statement. You've got a group of very experienced programmers donating their time and assistance at no charge. What you will find is a very low tolerance for people asking to have work done for them.
The goal of this board is to help make people better programmers, not to help them pass their IT classes with minimal effort. Approach this board with the right attitude and you'll find a great bunch of very helpful people. Come in here like the world owes you something and you'll get your attitude adjusted in short order. To the people who find that rude, all I can say is welcome to the real world.
-
Re: SRAND Number Generation Help
I can not believe people are still looking for (what looks like) rude/criticism type comments (to me) when they are plain on page one. If I need to point them out, fine:
"In despite of the fact that your code makes absolutely no sense..."
Saying I have no idea what I am doing? One reason why I came here in the first place, but I did not need to be have it told to me like that. Maybe JUST point out the error and solution instead of this method.
"What do you expect?"
I do not know. That is why I was asking in the first place.
"TruthSpeaker, your common sense is not with you, is it?"
My common sense is actually well, but thanks.
Whether other people see these as fine, great. Me, I do not. Whether my method of responding was too strong for some, then that I can apologize for.
GCDEF, you say about the real world. I have been in it for decades longer than I should, does not mean I have to accept society in any way others do. Does not mean I have to accept something negative, directly or unintentionally, from someone else if I choose not to. People will say the same thing to me when I am just ordering a sub at Subway.
If it helps, this was not for a class. This little thing I am doing is for myself trying to make a quick little text game to practice code on. This is where your statement "better programmers" come in for me, but I come here without needing what I see as witty type remarks from some. For anytime I am incorrect on someone's statements, all they need to do is explain their remarks and I may apologize if the situation is called. I never have time for text fights, even accidental ones. So again, if my methods of responding was too strong for some, then that I can apologize for.
This point on, please stay on topic about the generator. No more need to talk about other peoples comments at this point. Well, I see some other posts above I need to try out. Finding the time to try other methods, along with finding time to sleep, is a task, so please bare with me for responses once I have some about the new tips mentioned. It takes me weeks/months to get around to even play with a single line of code. Bare with me as I will come back in time and respond.
Thank you.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by
TruthSpeaker
It takes me weeks/months to get around to even play with a single line of code. Bare with me as I will come back in time and respond.
Thank you.
I'd suggest using that time to thicken your skin a little. Nobody was rude to you and to come back after the post has been dormant for two weeks seems a little :ehh:. You want love, call your mother. You want help, here we are.
-
Re: SRAND Number Generation Help
Sorry you think that, GCDEF, but your comment just now was also rude. Why do you people persist? Just talk to me with respect, not insults or negative comments, show me what I did wrong, not criticize, and all is good.
No worries. I forgave you all, just keep the topic in hand on hand and stop your fussing, all of you.
Dormant, like I said, it takes me a long time sometimes because I am always flying. That comment of yours was unnecessary too.
Like I said, knock it all off. If you want to complain, go to your parents please (just to throw in your advice which seems silly anyway considering my parents are deceased now).
As for the past suggestions, when I get to that computer, I will try them out and post back when I can.
Thank you.
-
Re: SRAND Number Generation Help
Quote:
Originally Posted by TruthSpeaker
Dormant, like I said, it takes me a long time sometimes because I am always flying. That comment of yours was unnecessary too.
You might to know that in some online communities, which I think includes this one, it is generally considered rude to revive a dormant thread.
What exactly constitutes rudeness depends on culture as well as the individuals involved. It so happens that the culture of various online communities tends to require "thick skin", perhaps because when people do not meet face to face and are relatively anonymous, they tend to speak more freely. So, just adapt to the culture, i.e., when in Rome, do as the Romans do.
-
Re: SRAND Number Generation Help
If you say so, but I do not see anything wrong with reviving a topic of my own for the ongoing issue I may/still have. If you do not approve, then do not worry about it.
In America, people like me see comments like others as "uncalled for" just because of the reality of experience.
Do not worry about it.