5

I want to be able to generate the following array (or something like it) in javascript:

[
  52, // random number 0-100
  53, // random +1 or -1
  54, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  51, // random +1 or -1
  50, // random +1 or -1
  51, // random +1 or -1
  // etc., etc., etc.
]

How can I do that?

I've tried this, but I always get a random number followed by 1's and -1's only:

Array(50).fill(0).map((v, i, a) => i !== 0 ? (Math.round(Math.random()) ? a[i-1] + 1 : a[i-1] - 1) : Math.floor(Math.random() * 101))
5
  • 1
    Is that ± 1 from the previous value? Or ± 1 from an initial, randomly generated value? Commented Nov 29, 2020 at 14:23
  • The question is not clear. What exactly are you tring to do? Commented Nov 29, 2020 at 14:25
  • @ItoPizarro It's from the previous value, however the array[0] is randomly generated Commented Nov 29, 2020 at 14:25
  • @odedbartov I'm trying to generate an array, starting with a random number from 0 - 100, for example 50, followed by either 50 + 1 (51) or 50 - 1 (49) (chosen randomly). For the next one, if 49 is chosen, it is followed by either 48 (49 - 1) or 50 (49 + 1), chosen randomly. Commented Nov 29, 2020 at 14:29
  • i posted an answer, check it out Commented Nov 29, 2020 at 14:52

4 Answers 4

2

This might help:

function randomGenerator(size) {
    let result = [];
    let firstValue = Math.round(Math.random() * 100);
    result.push(firstValue);
    
    for (let i=0;i<size-1;i++){
        firstValue += Math.random() > 0.5 ? 1:-1;
        result.push(firstValue)
    }
    return result;
}

console.log(randomGenerator(10));
console.log(randomGenerator(13));

or if you prefer to go functional you can take advantage of default values and comma operator:

const randGenerator = (size = 13,init = Math.round(Math.random() *100)) => 
Array(size).fill(0).map(e => (init += Math.random() > 0.5 ? 1:-1,init))

console.log(randGenerator(10))
console.log(randGenerator(13))

Sign up to request clarification or add additional context in comments.

Comments

0

Simple solution:

function rand(min, max) {
  return min + Math.floor((max + 1 - min) * Math.random())
}

function generate(size) {
  let last = rand(0, 100)
  const array = []
  for (let i = 0; i < size; i++) {
    array.push(last)
    last += rand(0, 1) ? 1 : - 1
  }
  return array
}

console.dir(generate(50))

Comments

0

Here is how you can do it with a recursive function, you can set the initial min and max for the random number and also specify the final array length:

const arr = [];

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min);
}

function updateArr(min, max, arrLength, arr) {
  if (arrLength) {
    let resultNum = getRandomInt(min, max);
    if (resultNum !== arr[arr.length - 1]) {
      arr.push(resultNum);
      updateArr(resultNum - 1, resultNum + 1, arrLength - 1, arr);
    } else updateArr(min, max, arrLength, arr);
  }
}

updateArr(0, 100, 10, arr);

console.log(arr);

Comments

-1
arr = [Math.random()*100]
for (let i = 0; i < 49; i++){
arr.push(arr[i] + (Math.random() > 0.5? 1 : -1))
}

Is this working for you?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.