Random spot of an array

So I have an array and need a random spot of it. The problem is, that I need that spot twice so I can't really use

random(ArrayName);
random(ArrayName);

because I would get two different spots. I also can't do

    var rand = int(random(0, 4));
    arrayName[this.rand];

because the syntax doesn't allow it. I hope you get my point and someone knows an answer. Thanks in advance.

Tagged:

Answers

  • PS: I can't mark the code pieces as code for some reason ...

  • edited May 2017

    Alright I managed to mark the pieces as code :)

  • Sorry, but I'm also confused by your question. Need to explain it better. :-\"

  • edited May 2017

    Ok so I have stored Vectors in that array and I need one of them (a random one) the problem is that I have to call that function twice because I need the x and the y value of that same Vector. That means I cant use

    random(ArrayName);
    random(ArrayName);
    

    twice because it would get me two different spots. I neither can use

    var rand = int(random(0, 4));
    arrayName[this.rand];
    

    which would get me the same spot twice (my goal) because the syntax doesn't allow it ... I guess.

  • Do you have two parallel arrays of type float?

    or one array of type PVector?

    Your second approach looks good to me:

    var rand = int(random(0, 4));
    arrayName[this.rand];
    point(arrayNameX[this.rand], arrayNameY[this.rand]); 
    
  • edited May 2017

    Still not getting it... Can't you just store the randomly picked p5.Vector in some temporary variable? :-/

    BtW, just made an online example sketch, so you can confirm whether that's what you're looking for. :-\"

    Visit here for it: http://CodePen.io/GoSubRoutine/pen/dWeaeg/left?editors=001

    /**
     * Vector Array Random Pick (v1.0)
     * GoToLoop (2017-May-13)
     *
     * forum.Processing.org/two/discussion/22573/random-spot-of-an-array#Item_7
     * CodePen.io/GoSubRoutine/pen/dWeaeg/left?editors=001
     */
    
    "use strict";
    
    const VECS = 10, MULT = 10, FRACT = 3,
          SIZE = 48, BOLD = 1.5,
          vecs = Array(VECS);
    
    let bg, cx, cy;
    
    function setup() {
      createCanvas(300, 200).mousePressed(redraw);
      noLoop();
    
      colorMode(RGB).fill('yellow').stroke('red');
      strokeWeight(BOLD).strokeCap(ROUND);
      textSize(SIZE).textAlign(CENTER, CENTER);
    
      bg = color('blue');
      cx = width >> 1, cy = height >> 1;
    
      for (let i = 0; i < VECS; vecs[i++] = p5.Vector.random2D().mult(MULT));
      console.table(vecs);
    }
    
    function draw() {
      const {x, y} = random(vecs),
            txt = `x = ${nfs(x, 0, FRACT)}\ny = ${nfs(y, 0, FRACT)}`;
    
      background(bg).text(txt, cx, cy);
    }
    
  • edited May 2017

    My complete code is:

    var points = [];
    var mover;
    var ax = 0;
    var ay = 0;
    var point2;
    var bx;
    var by;
    
    function setup() {
      createCanvas(1900, 1300);
    
      this.points.push(createVector(500, 100));
      this.points.push(createVector(1000, 966.026));
      this.points.push(createVector(1500, 100));
      this.mover = createVector(1000, 483);
    
    }
    
    function draw() {
      noStroke();
      fill(51);
      for (var i=0; i<points.length; i++) {
        ellipse(points[i].x, points[i].y, 20, 20);
      }
    
      noStroke();
      fill(255, 50, 50);
      ellipse(mover.x, mover.y, 5, 5);
    
      var rand = int(random(0, 4));
      this.ax = (points[this.rand].x + this.mover.x) /2;
      this.ay = (points[this.rand].y + this.mover.y) /2;
      this.mover.x = this.ax;
      this.mover.y = this.ay;
    }
    

    I cant use a variable to store the random value, because I can't use that variable to get a spot in the array ....

  • I can even use a variable with a set value like

    var var = 1;
    ArrayName[this.var].x;
    ArrayName[this.var].y;`
    

    But I can't use a variable in which a random value is stored (even if it is an int)

    var rand = int(random(0,4));
    ArrayName[this.rand].x;
    ArrayName[this.rand].y;
    
  • why not?

  • It throws an error saying something like "x is undefined". The problem is, that x is not undefined and when I use a variable which is not random it works. I can only get a random spot using:

    random(ArrayName);

    But then I can't use that same spot twice because the spot is not stored in a variable ...

  • edited May 2017

    Little Update:

    I just fixed the problem with a switch case with a variable of random value. It's a bit complicated but it works... Thank you for you help anyway

Sign In or Register to comment.