-2

So I found this useful website with a lot of code and explanation to perlin and simplex noise. However, the code is written in a different language. I was able to rewrite most of it for java, however there is one function that 1. I don't understand, and 2. I don't know how to write it in java. The code is:

function IntNoise(32-bit integer: x)             

    x = (x<<13) ^ x;
    return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);    

  end IntNoise function

Again, I don't know what language it is written in. However, the author states that the function returns a random number between -1 and 1. Can someone explain what exactly the & symbol does? And why there are a bunch of seemingly random numbers? And is there a simple way to convert this to java?

MagnusCaligo
  • 707
  • 2
  • 8
  • 28
  • 3
    Hint: What does `&` do in Java? –  Feb 23 '15 at 21:05
  • 1
    @JackManey The OP asks exactly that ("Can someone explain what exactly the & symbol does?") – nanofarad Feb 23 '15 at 21:06
  • 3
    Incidentally, unless used for code golf or some kind of obfuscated code contest, this is horrible, horrible code. Far better to just do `2 * Math.random() - 1`. –  Feb 23 '15 at 21:07
  • 3
    @hexafraction: And there is documentation to explain that. I'm trying to get the OP to learn how to fish instead of cramming another fish into his/her mouth. –  Feb 23 '15 at 21:07
  • Bear in mind, that code probably does not generate a random number between -1 and 1. It probably generates a random number between -1 and 1, with one end being inclusive and the other exclusive. That is, either -1 or +1 will be out of the range of generated numbers. – Tony Ennis Feb 23 '15 at 21:14
  • @TonyEnnis in the OP's example it is (-1, 1] but this is [-1, 1) but you can adjust this if really needed. – Peter Lawrey Feb 23 '15 at 21:30

2 Answers2

6

The inner part hashes the number, the & and / turns this hash into a number between 0 and 2 so when you do 1 - (..) you get a number between -1 and 1.

A Java way to get a random number between -1 and 1 is

return Math.random() * 2 - 1;

or if you need to use a seed

return new Random(x).nextDouble() * 2 - 1;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    Math doesn't have a constructor. Don't you mean `java.util.Random`? – nanofarad Feb 23 '15 at 21:11
  • You can use `Math.random()`, but only when not using a seed. See here: http://stackoverflow.com/questions/17445813/random-seed-math-random-in-java – mbomb007 Feb 23 '15 at 21:14
  • @PeterLawrey, your second code throws an error. `error: constructor Math in class Math cannot be applied to given types` and `required: no arguments found: int` – mbomb007 Feb 23 '15 at 21:20
  • I tried something similar to this before, but it didn't work. However I tried it again just now and it does lol I must have written it wrong before. I didn't use your code exactly, instead I created a new random, set the seed to x, and the returned the next double*2 -1 – MagnusCaligo Feb 23 '15 at 21:23
1

This language appears to have very similar operators but different declarations. The only important thing is to make Java interpret the bits as an IEEE-754 float. The following should suffice:

float rNoise(int x){
x = (x<<13) ^ x;
return ( 1.0 - Float.floatToIntBits( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0); 
}

The & operator will perform a bit-level AND operation on each bit of the number independently, while ^ will do an XOR operation in a similar fashion. It looks like the XOR is used for mixing and the & is used for masking.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 2
    I don't think it is a great idea to implement a random number generator if one does not understand random number generators. We don't know if the code's original author knows. I'd use the built-in function per @Jack Maney and trust peer review. Jack's version is also much easier to understand. – Tony Ennis Feb 23 '15 at 21:11
  • 1
    @TonyEnnis I'm addressing the OP's original questions: "Can someone explain what exactly the & symbol does? And why there are a bunch of seemingly random numbers? And is there a simple way to convert this to java?". – nanofarad Feb 23 '15 at 21:21
  • ^^^what @hexafraction said. Besides, there are plenty of reasons for implementing any algorithm from scratch, such as wanting to learn. I do this frequently. –  Feb 23 '15 at 21:22
  • 1
    I didn't down-vote you but it's still a bad answer to a poor question. – Tony Ennis Feb 23 '15 at 21:32