sundeep sharma
sundeep sharma

Reputation: 207

How to assign Randomly generated values inside of multiple object

I want to assign randomly generated numbers in multiple object keys but when I call one key value in another key then I am getting different values but I want to get same value as my previous key how I can do that, help me please.

function randomNum() {
    return {
        random: () => {
            return Math.floor(Math.random() * 100 + 1);
        },
        sameRandom: () => {
            return randomNum().random();
        },
    };
}

I want to get same number in random() and sameRandom().

Upvotes: 3

Views: 70

Answers (5)

vorillaz
vorillaz

Reputation: 6286

random and sameRandom methods are immediately invoked. You can either use closures or some sort of memoization, since each time calling random or sameRandom would produce a new number it's impossible to track down the order of execution, thus it makes sense to create a refresh method;.

function randomNum() {
  const getRandomNum = () => Math.floor(Math.random() * 100 + 1);
  let latestRandom = getRandomNum(); 
    return {
    refresh: () => latestRandom = getRandomNum(),
        random: () => {
            return latestRandom;
        },
        sameRandom: () => {
            return latestRandom;
        },
    };
}
const s = randomNum();
s.random(); // 62
s.sameRandom(); //62
s.refresh(); // 85

Upvotes: 2

Bryan Dellinger
Bryan Dellinger

Reputation: 5294

In addition to the answers given. you could just have random in your function. and then use a getter to reference it again.

run snippet below

function randomNum() {
    return {
        random: () => {
            return Math.floor(Math.random() * 100 + 1);
        },
    };
}

const myObj = {
        random:  randomNum().random(),
        get sameRandom(){
        return this.random
        }
      };
      
console.log(myObj);

Upvotes: 1

Majed Badawi
Majed Badawi

Reputation: 28424

You can set a global variable in random() and get it in sameRandom. In case sameRandom was called and it wasn't set yet, return random():

let rand = null;
function randomNum() {
  return {
    random: function() {
      rand = Math.floor((Math.random()*100)+1); 
      return rand; 
    },
    sameRandom: function() {
      return rand ? rand : this.random(); 
    }
  }
}
console.log(randomNum().random());
console.log(randomNum().sameRandom());

Upvotes: 1

The Bomb Squad
The Bomb Squad

Reputation: 4337

Firstly, same random would activate the function again.. if you want a random value to last, I changed it to this(still not sure why you don't just make const someOuterVariable=randomNum().random())

function randomNum() {
    let randVar=null
    return {
        random: () => {
            return randVar=Math.floor(Math.random() * 100 + 1);
        },
        sameRandom: () => {
            return randVar
        }
    };
}

let rand=randomNum()
console.log(rand.random())
console.log(rand.sameRandom())

Upvotes: 1

Endothermic_Dragon
Endothermic_Dragon

Reputation: 1187

Try something like this:

randomGenerator = {
  random: () => {
    randomNum = Math.floor(Math.random() * 100 + 1)
    randomGenerator.pastRandoms.push(randomNum)
    return randomNum;
  },
  pastRandoms: []
};

//First time
console.log(randomGenerator.random())
console.log(randomGenerator.pastRandoms)

//Second time
console.log(randomGenerator.random())
console.log(randomGenerator.pastRandoms)

When you're trying to access past randoms, it actually just generates a new random number. So, just create a 'history', and push every newly generated number into it, within the random function.

Upvotes: 1

Related Questions