Get Random 2D, 3D - Node.js Number

Node.js examples for Number:Random Number

Description

Get Random 2D, 3D

Demo Code

function getRandomPoints3D(numPoint, xMax, yMax, zMax) {
    var points = new Array();
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ xMax /4 + r * Math.cos(theta), yMax/2 + 2 * r * Math.sin(theta),  zMax/5 + 2 * r * Math.sin(theta)] );
    }/* w ww  . j  ava 2 s .com*/
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ xMax /4 * 3 +  r * Math.cos(theta), yMax/2 +  r * Math.sin(theta),  zMax /5 * 3 +  r * Math.cos(theta) ] );
    }
    return points;
}

function getRandomPoints2D(numPoint, xMax, yMax) {
    var points = new Array();
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ (xMax /4 + r * Math.cos(theta)) * Math.pow(-1, Math.round(Math.random())), 
                       (yMax/2 + 2 * r * Math.sin(theta)) * Math.pow(-1, Math.round(Math.random()))] );
    }
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ (xMax /4 * 3 +  r * Math.cos(theta))* Math.pow(-1, Math.round(Math.random())), 
                       (yMax/2 +  r * Math.sin(theta))* Math.pow(-1, Math.round(Math.random()))] );
    }
    return points;
}

function random2D(numPoints, maxX, maxY) {
  var points = new Array();

  for ( var i = 0; i < numPoints; i++) {
    
    points.push([Math.floor(Math.random() * maxX) * Math.pow(-1, Math.round(Math.random())), 
                 Math.floor(Math.random() * maxY) * Math.pow(-1, Math.round(Math.random()))]);
  }
  
  return points;
}

function random3D(numPoints, maxX, maxY, maxZ) {
  var points = new Array();

  for ( var i = 0; i < numPoints; i++) {
    
    points.push([Math.random() * maxX * Math.pow(-1, Math.round(Math.random())), 
                 Math.random() * maxY * Math.pow(-1, Math.round(Math.random())),
                 Math.random() * maxZ * Math.pow(-1, Math.round(Math.random()))]);
  }
  
  return points;
}

function random4D(numPoints, maxX, maxY, maxZ, maxW) {
  var points = new Array();

  for ( var i = 0; i < numPoints; i++) {
    
    points.push([Math.random() * maxX * Math.pow(-1, Math.round(Math.random())), 
                 Math.random() * maxY * Math.pow(-1, Math.round(Math.random())),
                 Math.random() * maxZ * Math.pow(-1, Math.round(Math.random())),
                 Math.random() * maxW * Math.pow(-1, Math.round(Math.random()))]);
  }
  
  return points;
}

function random4DPos(numPoints, maxX, maxY, maxZ, maxW) {
  var points = new Array();

  for ( var i = 0; i < numPoints; i++) {
    
    points.push([Math.floor(Math.random() * maxX) + 1, 
                 Math.floor(Math.random() * maxY) + 1,
                 Math.floor(Math.random() * maxZ) + 1,
                 Math.floor(Math.random() * maxW) + 1]);
  }
  
  return points;
}

Related Tutorials