How to get rid of predictable random() results

edited August 2017 in Library Questions

Hey guys,

I am trying to make a surface using ISurface command of igeo library and I used a 2d array of vector points to create the surface and I set a random value for z parameter of each point. but it appears random function is not that much random and it generates repetitive values. so anyone knows how to get really random values?

Tagged:

Answers

  • edited August 2017
    import processing.opengl.*;
    import igeo.*;
    
    size(800, 600, IG.GL);
    int numU = 100;
    int numV = 100;
    
    IVec[][] crv = new IVec[numU][numV];
    
    for (int i=0; i<numU; i++) {
      for (int j=0; j<numV; j++) {
        crv[i][j] = new IVec(i*2-100, j*2-100, random(1)*5*(sin(i*0.2)+1)); 
      }
    }
    new ISurface(crv);
    
  • What makes you think you aren't getting random values?

  • What makes you think you aren't getting random values?

    Because as you see in image there is a constant cycle that changes all values in each row in U direction : form 0 to near 5 and 0 again.

  • edited August 2017 Answer ✓

    You may want to look at section 1.6+ on this page (Excersize 1.10 in fact is what you're trying to do) and instead use perlin noise instead. With a sin wave, you'll consistently be multiplying a random number by a repeating variable, which over time will have the same general trend. This is even more of a problem when multiplying near the zeros of the sin wave, since the value i will be multiplied by will be extremely small, which will lead to that repeating flat ground pattern.

    Perlin noise uses different clever math that makes a lot more natural looking random landscape which I believe is what you're looking for.

  • Answer ✓

    Try:

    random(1)*5
    

    instead of:

    random(1)*5*(sin(i*0.2)+1)
    

    on line 12.

    Does that fix it?

  • You were right TfGuy44! I made a silly mistake

Sign In or Register to comment.