I have a PRNG-Class called Generator. Now, after creating a new instance of it:

var g:Generator = new Generator();

Custom function: set_seed() - Please review this approach.

I call g.set_seed(); which has the following logic:

public function set_seed(s:Number = 0):void
    seed = (s > 0) ? s : new Date().getTime();
  • Pro: I can call it without any arguments, looks more beautiful in the classes I'm using it.
  • Con: set_seed looks awful and I feel I should be using Flash's Getter/Setter approach.

(I'm getting the seed with: get_seed();)

  • \$\begingroup\$ What's the request here? It doesn't appear that you're seeking a code review. \$\endgroup\$ – Jamal May 9 '14 at 18:17
  • \$\begingroup\$ I want to get my Version with "set_seed()" reviewed. \$\endgroup\$ – user42193 May 9 '14 at 18:20
  • \$\begingroup\$ Okay. I'm not sure if there's much code here to review, but others can determine that. \$\endgroup\$ – Jamal May 9 '14 at 18:21

I would expect the generator to seed itself (using the current time) in its constructor.

Therefore, I would not use a default parameter in your set_seed method.

Also, in your current code, you treat all the negative values the same as zero. I wouldn't recommend that. Many random seeders can handle negative seeds as well, if you don't want to do that: Throw an Exception/Error!

Therefore, I would use this:

public function set_seed(s: Number): void
    if (s < 0) {
        throw new Error("Seed must be positive");
    seed = s;

For comparison and inspiration, check Java's java.util.Random class. Note that it has two constructors available, one for initializing to System's seed and one for initializing to a specific seed. That is an approach I would recommend.

  • \$\begingroup\$ Thanks alot for your fast answer; If I want to change the seed later, should I create a new Instance or would you suggest changing the value with Flash's Getter/Setter, my own or directly? \$\endgroup\$ – user42193 May 9 '14 at 18:54
  • \$\begingroup\$ @gonida I only know limited things about what you are making. Do what you feels best. You can still have a set_seed method if you'd like (note that the Java class also has setSeed). \$\endgroup\$ – Simon Forsberg May 9 '14 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy