1

As above, I have an array. Let's say for example [12, 24, 89, 09, 43, 99].

For a user, I want to select the same numbers from the array each time they visit, but different numbers for other users.

The only possible way I can think is to do it based on their session ID which looks like:

"{6051c670-5122-4f6c-95ff-5e6c538ed5c0}"

Any ideas on how this could be achieved would be helpful.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • You can use the user id. It should be unique by user. – R3tep Jun 24 '19 at 14:44
  • Forgot to mention, cannot store the values in a cookie as the user would build up a lot of storage as they move through the site. – Michael Sherris Caley Jun 24 '19 at 14:45
  • Have you considered `localStorage`? mdn: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage – junvar Jun 24 '19 at 14:46
  • 1
    I can't say i am giving an answer to your question due to the lack of information on why you're trying to achieve this. It sounds like there is a better solution to whatever you're trying to do. However, a couple things you should consider are server cache and SQLite. Other than that, you might as well do this in a db. – FernandoG Jun 24 '19 at 14:52
  • Can you give more information please? Like how long is the real array of random numbers? Why can't you use the userId (which would be unique to each user)? Does there need to be a special pattern in the picks from the aray? – MauriceNino Jun 24 '19 at 14:55
  • junvar Storing the result is not an option I'm afraid. Would require storage on too many pages. @MauriceNino The length of the array doesn't matter. It just matters that you're getting the same result for the same individual – Michael Sherris Caley Jun 24 '19 at 16:05
  • The length might matter for the solution tho. Also, how many numbers should be picked from the array? – MauriceNino Jun 25 '19 at 08:49

1 Answers1

1

You can use a seedable random numbers generator like seedrandom

How it works:

Every random number generator is only a psuedo random generator, meaning nothing is really random here, even for Math.random - it's just some crazy algorithm that generates numbers by a given input.

But javascript seeds Math with its own variables and doesn't allow us to seed it ourself (like we can in c for example) - that's where seedrandom comes in, it's just a simple custom random number generator for javascript, which also happens to accept strings as a seed (good for your needs).

We feed the random number generator a custom seed (user id/session) which allows us to have a predictable random number generator - so for example if the first call produce 0.2, and the second call produce 0.5 - we can be sure that if the user refreshes the page and we give the same seed, the first number will always be 0.2 and the second number will always be 0.5.

How to use:

The use is pretty easy:

// Make a predictable pseudorandom number generator.
const seedrandom = require('seedrandom');

const myRandom = seedrandom(sessionID);
console.log(myRandom());  // Always 0.9282578795792454

// example
const pickRandomForUser = (items) => {
  const predictableRandom = myRandom();
  const randomIndex = Math.floor(predictableRandom * items.length);
  return items[randomIndex];
}

const numbers = [12, 24, 89, 09, 43, 99];

// will stay the same for each session id, in this order
const firstRand = pickRandomForUser(numbers)// 12
const secondRand = pickRandomForUser(numbers)// 89
Liron Navon
  • 1,068
  • 1
  • 11
  • 22
  • 1
    This looks like the exact type of thing I was looking for. I guess this would be difficult to achieve without the use of the framework? – Michael Sherris Caley Jun 24 '19 at 16:06
  • You only need two things: 1. generate a number from the user identifier (if you have a numeric ID, use that) 2. create a random numbers generator that can be seeded. You can create your own custom number generator, if it fits your purpose and you need something super simple... , check this answer - https://stackoverflow.com/a/19303725/6012724. You can create a number from the UUID using a simple non secure numeric hash function - https://stackoverflow.com/a/7616484/6012724 – Liron Navon Jun 25 '19 at 12:02