Member Avatar for Smartflight

Yes, I did a search, and I understand time() better than before, but I would appreciate personal help.

Initially, I wanted to make a program/function that would randomly generate a number. Next, I wanted to limit it between two defined numbers. I came across rand() yesterday, and I couldn't get it to work. I realized rand() had to be seeded with the function srand(), and the most preferred source was time, following being the code

#include <time>
#include <iostream>
#include <cstdlib>

int main()
{
 int random;
 srand(time(null));
 random=rand() % 51;
 cout<<"\nA random number not greater than 50? Well, what about "<<random<<"?";
 return 0;
}

My doubt is at line 9. From what I have studied in my school so far, % finds and assigns the remainder of an expression. So, if time is calculated since 1-Jan-1970, how does dividing the seconds passed by until that day by 51, give a number between 0 and 50?

Or is this a piece of code specifically for rand() function?

Recommended Answers

All 10 Replies

The time() function is not used by the rand() function. It's only a way to seed the srand() function with a different value every time it's called. rand() returns a value from 0 to RAND_MAX (32767 on many systems).

Member Avatar for Smartflight

Ok, so time() function (argument being null) returns what?

Is there any communication between rand() and srand() function?

Also, how do you define seeding?

The code for srand() and rand() is

void __cdecl srand (unsigned int seed)
{
#ifdef _MT
  _getptd()->_holdrand = (unsigned long)seed;
#else  /* _MT */
  holdrand = (long)seed;
#endif  /* _MT */
}

int __cdecl rand (void)
{
#ifdef _MT
  _ptiddata ptd = _getptd();
  return( ((ptd->_holdrand = ptd->_holdrand * 214013L   + 2531011L) >> 16) & 0x7fff );
#else  /* _MT */
  return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif  /* _MT */
}

So if you look at the rand function you will see that the function generates a number from a formula based on a parameter(holdrand). If the parameter is unchanged it will generate the same numbers at every execution so to change the parameter you are using srand(), but if the parameter does not change it will still generate the same numbers between program executions. So to constantly change the parameter the rand function is based on you can set the parameter with the time variable which will never be the same between two different executions.

so time() function (argument being null) returns what?

An unspecified arithmetic type containing the current system time in an unspecified format. Technically, using time to seed srand is non-portable due to the unspecifiedness of the time_t type and value.

Is there any communication between rand() and srand() function?

rand() uses and maintains the seed generated by srand().

Also, how do you define seeding?

Clicky.

>>Ok, so time() function (argument being null) returns what?

It returns the number of seconds since 1970, as you previously posted. It doesn't matter whether the parameter is NULL or not, it will always return the same value.

It returns the number of seconds since 1970

Perhaps on your implementation. Here's the specification for the time() function in its entirety:

7.23.2.4 The time function

Synopsis

1 #include <time.h>
time_t time(time_t *timer);

Description

2 The time function determines the current calendar time. The encoding of the value is
unspecified.

Returns

3 The time function returns the implementation’s best approximation to the current
calendar time. The value (time_t)(-1) is returned if the calendar time is not
available. If timer is not a null pointer, the return value is also assigned to the object it
points to.

Unspecified means the implementation can do whatever it wants as long as the choice is documented. Nowhere in the standard is "1970" or "epoch" mentioned. Further, because the representation of time_t is unspecified, the result of time() need not be in seconds. An implementor can choose to make time_t the number of Twinkies sold in the fiscal year as long as the other date/time functions behave as specified.

commented: absolutely correct +36
Member Avatar for Smartflight

I should have mentioned- I am a beginner.

So, what I have noted is

1) The value returned by time() function is implementation based, and not strictly seconds passed since 1970.
2) rand() uses and maintains the seed generated by srand().

I am not clear as to what a seed is, I do not understand the source code of the functions srand() and rand() But thanks for the help guys. For now, I'll have to partially learn this part of the code, and later when I have a better understanding of C++, I will dig the two functions, along with time() .

I am not clear as to what a seed is

In the case of rand(), you can think of the seed as the starting point for generating pseudorandom numbers. Since pseudorandom numbers are deterministic, there has to be some initial value for the algorithm.

Its up to you to disprove it.

I already did. The standard requires nothing of the sort. In fact, the standard goes out of its way to make the representation unspecified. Seeing as how I could fairly easily disprove your claim by writing my own implementation that does something different, I wouldn't recommend putting the ball in my court.

>>I am not clear as to what a seed is
I think the easiest way to think about it, from a beginner's perspective, is that, since a computer is deterministic (it can't actually produce truly random numbers), the best it can do is use a formula that produces "very wacky" results. In other words, the numbers generated by the formula make a very jumpy sequence of numbers where each number doesn't really seem (to the naked eye) as having anything to do with the previous one. But this is still a deterministic sequence, because if you start at the same place in the sequence, you will always get the same sequence of following numbers. The seed is, simply put, where you start in that sequence. And to get "random" number sequences, you need to start with a "random" seed. Since the exact time at which a program is started is pretty unpredictable and arbitrary, it's a fairly good "random" number to start with.


[edit]
for any comments that seem to be made without a previous post in this thread, see this discussion
[/edit]

@AD:>>Its up to you to disprove it.
No. You are claiming that all implementations of time() are the same, you have the burden of proof (a pretty heavy one too).

@Narue:>>I wouldn't recommend putting the ball in my court.
Well said!

commented: Well explained! +1
Member Avatar for Smartflight

>>I am not clear as to what a seed is
I think the easiest way to think about it, from a beginner's perspective, is that, since a computer is deterministic (it can't actually produce truly random numbers), the best it can do is use a formula that produces "very wacky" results. In other words, the numbers generated by the formula make a very jumpy sequence of numbers where each number doesn't really seem (to the naked eye) as having anything to do with the previous one. But this is still a deterministic sequence, because if you start at the same place in the sequence, you will always get the same sequence of following numbers. The seed is, simply put, where you start in that sequence. And to get "random" number sequences, you need to start with a "random" seed. Since the exact time at which a program is started is pretty unpredictable and arbitrary, it's a fairly good "random" number to start with.

That helps a ton! Thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.