initialisation rand seed

P

Philipp Kraus

Hi,

I'm implementating some algorithems with random data, so I must
initialisation the seed. But the code creates many objects of my class
so that the seed, which is set to
static_cast<std::time_t>(time(0)))
is in all objects the same and so I get in all objects the same "random
values".

My idea for solving this seed problem, ist to mix the time with the
object reference (*this).
Is this a good solution or do you know another algorithm? How can I
convert the reference to a numeric value?

Thanks

Phil
 
F

Francesco S. Carta

Hi,

I'm implementating some algorithems with random data, so I must
initialisation the seed. But the code creates many objects of my class
so that the seed, which is set to
static_cast<std::time_t>(time(0)))
is in all objects the same and so I get in all objects the same "random
values".

My idea for solving this seed problem, ist to mix the time with the
object reference (*this).
Is this a good solution or do you know another algorithm? How can I
convert the reference to a numeric value?

First of all,I don't see the need to cast the result of time(), as it
already is a time_t.

Further than that I cannot see the problem. If you need different seeds
just call time() again for each new instance of your classes (instead of
duplicating the first seed, as you seem to be doing).
 
P

Philipp Kraus

First of all,I don't see the need to cast the result of time(), as it
already is a time_t.

Further than that I cannot see the problem. If you need different seeds
just call time() again for each new instance of your classes (instead
of duplicating the first seed, as you seem to be doing).

This doesn't work, time() returns on every call the same value, so all
seed are equal. I need different seeds, so my idea I call time and mix
this value with my object reference, the object reference should be
different for every object.
 
F

Francesco S. Carta

Just initialize the seed once at the top of your main function rather
than in each object. Alternatively use something better than srand/rand
such as the tr1/boost PRNGs.

He makes no mention about which PRNG he is using. I suggested him to get
different seeds because I thought he was using something different from
srand/rand.

Indeed, if he uses the standard C rand(), just setting the seed once
with srand() at the beginning of the program would be enough.
 
F

Francesco S. Carta

This doesn't work, time() returns on every call the same value, so all
seed are equal. I need different seeds, so my idea I call time and mix
this value with my object reference, the object reference should be
different for every object.

No, time() returns different values at each call, assuming you let pass
at least one second from one call to the other ;-)

If you're using rand(), just call srand(time(0)) once at the beginning
of your program, although you would be better finding a better PRNG as
Leigh suggested.
 
F

Francesco S. Carta

What if both objects are created within the same second resulting in
time() returning the same for each? The traditional approach is to call
srand just the once at the top of main() or similar. A better approach
is to dump srand/time and use separate PRNG instances in each object
(using tr1/boost PRNGs).

You're right, I forgot to point out that "second granularity" issue. I
assumed some things about the original post and those things mislead my
reply.
 
F

Francesco S. Carta

Hi,

I'm implementating some algorithems with random data, so I must
initialisation the seed. But the code creates many objects of my class
so that the seed, which is set to
static_cast<std::time_t>(time(0)))
is in all objects the same and so I get in all objects the same "random
values".

My idea for solving this seed problem, ist to mix the time with the
object reference (*this).
Is this a good solution or do you know another algorithm? How can I
convert the reference to a numeric value?

As an aside from all the other replies, since it could be a good thing
to have different seeds in order to have different and reproducible
random sequences, you could, first of all, get a PRNG that you can
instantiate separately.

Once you have the first instance, set its seed from time(), and then
generate the further seeds from that first instance, seeds to be passed
to the further instances of the PRNG contained in your classes.

Doing things in this way you'll have a single originating seed for each
run of your program, a seed that you could eventually reuse to debug the
program or to simply reproduce past runs.
 
F

Francesco S. Carta

I meant "srand/rand" not "srand/time", but I think I posted too quickly
as OP didn't mention he was using srand/rand, I just assumed. xoring
"this" pointer with the time() might be an acceptable approach if
separate PRNG instances are being used.

We both posted quickly and we both assumed, but your guess was right. He
definitely seems to be using rand().

In any case, yes, the best way about randomizing stuff goes away from
rand() - shall anybody need a rationale for such "taking the distances"
from rand(), the problem is that rand() does not usually return good
random numbers, but moreover, its problem is about the fact that it
returns a single flow of random numbers, while having independent flows
can be very useful. This is achieved using a PRNG that can be
instantiated separately.
 
J

Jonathan Lee

My idea for solving this seed problem, ist to mix the time with the
object reference (*this).
Is this a good solution or do you know another algorithm? How can I
convert the reference to a numeric value?

There are some other options available to you. In my experience, the
"this" pointer isn't very random, though it'll help with the poor
resolution of time(). Instead of XOR-ing it (I assume this is what
you mean by 'mix'), concatenate the two values in an unsigned char
array and run some sort of hash on the result (ex., SHA-1). Then take
a word of the hash. Or use the two values as inputs to a decent
lagged fibonacci generator and run it for awhile (say, 1000
iterations)
then use that as your seed.

On *nix you have the option of reading from /dev/random or
/dev/urandom for a seed. Windows has some APIs for PRNGs.

--Jonathan
 
P

Philipp Kraus

There are some other options available to you. In my experience, the
"this" pointer isn't very random, though it'll help with the poor
resolution of time(). Instead of XOR-ing it (I assume this is what
you mean by 'mix'), concatenate the two values in an unsigned char
array and run some sort of hash on the result (ex., SHA-1). Then take
a word of the hash. Or use the two values as inputs to a decent
lagged fibonacci generator and run it for awhile (say, 1000
iterations)
then use that as your seed.

On *nix you have the option of reading from /dev/random or
/dev/urandom for a seed. Windows has some APIs for PRNGs.

--Jonathan

I've written this idea, it works. I hope my algorithm works with the
random numbers, but this I will see in the next days

Thanks
 
Ö

Öö Tiib

Hi,

I'm implementating some algorithems with random data, so I must
initialisation the seed. But the code creates many objects of my class
so that the seed, which is set to
static_cast<std::time_t>(time(0)))
is in all objects the same and so I get in all objects the same "random
values".

My idea for solving this seed problem, ist to mix the time with the
object reference (*this).
Is this a good solution or do you know another algorithm? How can I
convert the reference to a numeric value?

I think that if you need different random seeds then why not to
generate them with different random generator? Major bonus about
pseudo random generators is that you can reenter same (seemingly
random) situation later again. That is often needed for testing and
debugging, so avoid achieving random situations that you can not
reproduce.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top