Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How do I make the seed work for 2^32 or 2^64 numbers? #15

Closed
AndrewMast opened this issue Feb 25, 2016 · 8 comments
Closed

Question: How do I make the seed work for 2^32 or 2^64 numbers? #15

AndrewMast opened this issue Feb 25, 2016 · 8 comments

Comments

@AndrewMast
Copy link

How do I make the seed work for 2^32 or 2^64 numbers?

@josephg
Copy link
Owner

josephg commented Feb 25, 2016

Sorry, I'm not sure I understand what you're trying to do.

On Friday, 26 February 2016, Andrew Mast notifications@github.com wrote:

How do I make the seed work for 2^32 or 2^64 numbers?


Reply to this email directly or view it on GitHub
#15.

@AndrewMast
Copy link
Author

How do I change this function to work for 2^32 or 2^64 numbers?

  // This isn't a very good seeding function, but it works ok. It supports 2^16
  // different seed values. Write something better if you need more seeds.
  module.seed = function(seed) {
    if(seed > 0 && seed < 1) {
      // Scale the seed out
      seed *= 65536;
    }

    seed = Math.floor(seed);
    if(seed < 256) {
      seed |= seed << 8;
    }

    for(var i = 0; i < 256; i++) {
      var v;
      if (i & 1) {
        v = p[i] ^ (seed & 255);
      } else {
        v = p[i] ^ ((seed>>8) & 255);
      }

      perm[i] = perm[i + 256] = v;
      gradP[i] = gradP[i + 256] = grad3[v % 12];
    }
  };

@josephg
Copy link
Owner

josephg commented Feb 25, 2016

Why do you want that? Do you simply want more variety in the output?

@AndrewMast
Copy link
Author

I want to be able use this:

String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length === 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};

@AndrewMast
Copy link
Author

So basically I want to know what to do with the module.seed to make it work with 2^32 numbers (So I can use noise.seed("seed".hashCode());)

@Squarific
Copy link

You can take your 2^32 number and divide it by 2^32 then use that.

@AndrewMast
Copy link
Author

With this?

String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length === 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash / Math.pow(2, 32);
};

@Squarific
Copy link

This should work yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants