I'm trying to find a set of random numbers between 1 and 365, so I tried to use:

`DayOfBirth = rand() % 365 + 1;`

But, I've been told that srand() will produce a better random number, is this true? How do I use it for this instance?

PDB1982
0
Posting Whiz in Training

I'm trying to find a set of random numbers between 1 and 365, so I tried to use:

`DayOfBirth = rand() % 365 + 1;`

But, I've been told that srand() will produce a better random number, is this true? How do I use it for this instance?

Jump to Posta good one is the mesner twister

It's called the Mersenne Twister.

Just a quick note on the cast in my srand call -- srand ideally takes an unsigned int but time comes back with a one that is of type time_t, which is normally a typedef of integer. …

Jump to Post>>

But, I've been told that srand() will produce a better random numberWhomever told that to you should not talk about programming anymore.

All srand does is seed the random number generator.

>>a good one is the mesner twister

>>It's called the Mersenne Twister.Yes that is …

jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster

`#include <ctime>`

Then anytime before you use rand put this statement into your program once: `srand((unsigned)time(0));`

This gives rand a seed based on the current time.

Edited
by jonsca because:
*
n/a *

NathanOliver
429
Veteran Poster
Featured Poster

srand() seeds the rand() function. a good way to get a random seed is to use the time function because it changes every time you use it. it should be used as follows for general uses

```
//...
srand(time(NULL));
value = rand() % 365 + 1;
//...
```

you will need to include ctime in order to do this. for better and and more reliable random number generators i would search Google. a good one is the mesner twister.

Edited
by NathanOliver because:
*
n/a *

jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster

a good one is the mesner twister

It's called the Mersenne Twister.

Just a quick note on the cast in my srand call -- srand ideally takes an unsigned int but time comes back with a one that is of type time_t, which is normally a typedef of integer. srand() may generate a warning otherwise.

Edited
by jonsca because:
*
time_t *

NathanOliver
commented:
thanks for the corrction
+1

NathanOliver
429
Veteran Poster
Featured Poster

jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster

mrnutty
761
Senior Poster

>>**But, I've been told that srand() will produce a better random number**

Whomever told that to you should not talk about programming anymore.

All srand does is seed the random number generator.

**>>a good one is the mesner twister
>>It's called the Mersenne Twister.**

Yes that is better than rand, however usually, rand should suffice for

newbies.

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.