Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

dumb questions: seeding Tausworthe generator

Name: derp 2010-03-22 16:20

I'm not even sure if this code is right because I've only been able to find a couple snippets about the Tausworthe Generator on Google, neither of which were actually working implementations.

#include <stdio.h>
#include <time.h>
int s1, s2, s3;

int taus_gen(void) {
        s1 = ( (s1 & 0xFFFFFFFE) << 12) ^ (( (s1 << 13) ^ s1) >> 19);
        s2 = ( (s2 & 0xFFFFFFF8) << 4) ^ (( (s2 << 2) ^ s2) >> 25);
        s3 = ( (s3 & 0xFFFFFFF0) << 17) ^ (( (s3 << 3) ^ s3) >> 11);
        return s1 ^ s2 ^ s3;
}

int main(void) {
        int i;
        s1 = time(NULL);
        s2 = time(NULL);
        s3 = time(NULL);
       
        for (i=0;  i < 10;  i++)
                printf("%i\n", taus_gen());

        return 0;
}


1. Is this code a correct implementation of a Tausworthe generator?

2. Is simply using the time, ie. through time(), the best way to seed the three state variables?

I am now noticing there there's a visible pattern to the numbers that I output from one run to the next...
2575887790
2366031142
4046607044

2575879598
2399585702
4047131270

2578000942
2433154598
3108229496

Perhaps I am doing it wrong...

Name: Anonymous 2010-03-22 16:24

I am now noticing there there's a visible pattern to the numbers that I output from one run to the next...

Just realized that this is probably completely irrelevant provided that the numbers are random when I use it to modulo against the upper bound of whatever I want, ie. int n = taus_gen() % 10 when I want 0..9.

Name: Anonymous 2010-03-22 16:24

s1, s2 and s3 are all seeded as more or less the same number.

Name: Anonymous 2010-03-22 18:01

The value that time(NULL) returns doesn't change immediately. If, for example, I were make a program that prints out the value of time(NULL), then repeats once (without a delay), it is likely that it will have printed the same number twice.

Basically, you're seeding the same values, over and over again. You need to incorporate some form of delay between your seeds.

Name: Anonymous 2010-03-22 18:01

>>4

Oh, and to be precise, I think the value of time changes once every second.

Name: Anonymous 2010-03-22 18:35

>>5
Actually, the value of time changes once every 5.4x10-44s.

Name: Anonymous 2010-03-22 19:27

>>1
You're correct; those are dumb questions.

http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html is the only resource you need for the majority of the randomization algorithms. Implementations are typically simple, as in your example.

Of course there's a pattern. It's a stateful generator. There's supposed to be a pattern. Furthermore, you fucked it up. It relies on the properties of unsigned integers. You need to make those ints unsigned ints.

Name: Anonymous 2010-03-22 19:38

>>7
In addendum, your second question can be answered by the second version of the Tausworthe seeding algorithm. Instead of raw bits, it uses the following:

void Tausworthe::reset(uint32_t seed1, uint32_t seed2, uint32_t seed3) {
    m_seed1 = seed1 | 0x100000;
    m_seed2 = seed2 | 0x1000;
    m_seed3 = seed3 | 0x10;
}


Obviously SEPPLES, so you'll need to modify for C, but you should get the picture. The seeds can be anything you want, but I recommend using srand(time(0)); and then using three calls to rand() to get the seeds.

Name: derp OP 2010-03-22 20:17

Yeah, I know that I am essentially setting all 3 state variables to the same number.  I'm just wondering whether the current time is an OK seed for this particular algorithm.  I since talked to someone else who knows about this algorithm and they said I could use pretty much anything I want as long as I didn't use some really low values.

So as long as my code is correct, my question should be solved. :)

Name: derp OP 2010-03-22 20:20

I'm just wondering whether the current time is an OK seed for this particular algorithm.

And whether it would be ok to use the same seed for all 3 states.

>>8
Ok, good advice.

>>7
Will do.

Name: Anonymous 2010-03-22 20:43

You are now seeding manually.

Name: Anonymous 2010-03-22 22:12

>>11
PLANT YOUR SEED DEEP WITHIN MY ANUS

Name: Anonymous 2010-03-22 22:49

>>12
Sorry everyone, but I lol'd.

Name: Anonymous 2010-03-23 0:41

>>12
Hold it there Kodak! Are you an queer!

Name: Anonymous 2010-03-23 9:49

This only works if the integers are 32bit long.

Name: Anonymous 2010-03-23 10:34

>>15
This. It's not 1970 anymore, use uint32_t dude.

Name: Anonymous 2010-03-23 10:35

>>16
Its 1971?

Name: Anonymous 2010-03-23 11:07

or use s1 & (INT_MAX-1) or so but that would mean you'd have to compile a 32 and a 64bit version..
But >>16-san's advice is better..

Name: derp OP 2010-03-23 13:16

>>16
Yep, doing that now too, thanks

Name: Anonymous 2010-03-23 14:07

>>16
way to waste your fingers away
typedef uint32_t u32;

Name: Anonymous 2010-03-23 14:23

>>20
Wrong way round. TML.
I really need to get the balance right between the only two languages I both know and like, and Perl sure doesn't have typedef.

Name: Anonymous 2010-03-23 14:32

>>20
Add it to void.h

Name: Anonymous 2011-02-03 6:58

Don't change these.
Name: Email:
Entire Thread Thread List