2

I am creating fake statistics real-time data for that I need to show the number of users available on the website for one game of 5 minutes, I want to generate random numbers within a range of 1 to 100, but in incremental order. Which means every number that I get should be greater than the previous one.

Expected output: Output at each function call will be like 1,5,12,25,...,98,100 in randomized and incremental order only. Thanks in advance.

My code:

randomnumber(fixed,count,range){
  return fixed+count*range;
},
function(){
  var count = 1; 
  this.socketData( ({ data }) => { 
    count++ 
    data.data.totalUsers += this.randomnumber(50,count,Math.abs(Math.floor(Math.random() * (100 - 1) +1)));
}
Ritesh Naik
  • 103
  • 4
  • Does this answer your question? [Generating random whole numbers in JavaScript in a specific range?](https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) – VietDD Sep 17 '20 at 03:30
  • How many numbers do you want to generate? – Phil Sep 17 '20 at 03:31
  • 3
    Keep cranking up the minimum in your range each time you generate a value. Loop and done. Try and refocus this code on just that problem, as there's other stuff here (`socketData`?) that seems irrelevant. – tadman Sep 17 '20 at 03:31
  • @Phil range from 50 to 200 I need 60-70 numbers – Ritesh Naik Sep 17 '20 at 03:52
  • Do you need 60 or 70 numbers? Producing a variable count would be quite difficult. Why does your question mention a different range? Is it between 50 and 200 or 1 and 100? – Phil Sep 17 '20 at 03:55
  • 1
    If you don't allow duplicate numbers (i.e. each number must be greater than previous one), then the max numbers you can have is 100... and if you get the random number "100" on the first go, then you can't have any more numbers - correct? – Nick Grealy Sep 17 '20 at 04:06
  • @phil the question was just for example purpose, actually I need it between 50 to 200. It will be very helpful if you could help me with this. – Ritesh Naik Sep 17 '20 at 06:21
  • @NickGrealy Yes that's why I want the random number to pick start from 1 to 100 in incremental order only. – Ritesh Naik Sep 17 '20 at 06:22

3 Answers3

1

Assumptions

So, some assumptions to state:

  • no numbers can be duplicated
  • numbers must be ascending
  • numbers must be in range 1 - 100 (inclusive)

Which means we can have anywhere from 1x entry i.e. [100], up to 100 entries [0,1,2,...98,99,100].

So, to implement this strategy, we'll need to:

  • create X random entries (modify the seed as desired - 100x means we allow for all scenarios, I've chosen to randomise it)
  • sort the results
  • remove duplicates

TLDR;

Below is the implementation - works just as well for the range 50 - 200:

// range inclusive
const minRange = 1
const maxRange = 100
const rangePlusOne = maxRange - minRange + 1

// seed = [1 - 100]
const seedNumberOfEntries = parseInt(Math.random() * rangePlusOne) + 1

// generate, sort, unique
console.log([...new Set(
  new Array(seedNumberOfEntries)
    .fill(0)
    .map(() => parseInt(Math.random() * rangePlusOne) + minRange)
    .sort((a,b) => a-b)
  )]
)
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
  • OP didn't necessarily say they want 10 (or any specific count) numbers – Phil Sep 17 '20 at 03:34
  • @Phil - thanks for clarifying. I assumed OP wanted a finite, in-memory array of numbers. An infinite stream of numbers would require a different solution. – Nick Grealy Sep 17 '20 at 03:40
1

I suggest you create them in one loop and add to an array then sort that array. This should get you a sorted list of incremental random numbers.

You can then just pull 'next' whenever you want the next one.

let numbers = [];

for (let i =0, l = 100; i<l;i++){
  let number = Math.floor(Math.random() * 100);
  numbers.push(number);
}

 numbers.sort(function (a, b) {
   return a - b;
 });
 
 console.log(numbers);
Mark Taylor
  • 1,128
  • 8
  • 15
0

const from = 0;
const to = 100;
const rand = Number(String(Math.random()).slice(2, 2 + 15));
const num = from + (rand % (to - from - 1)) + 1;
console.log(num);

Hope this will helps you can give range from & to

pgksunilkumar
  • 236
  • 2
  • 5
  • Thanks for this brilliant effort, but this doesn't solve my problem because some times number produce is less than the number produce before which is not actually acceptable in my case. If the current number generated is 20 then next time onwards the number should always be greater than 20 and less than 100. – Ritesh Naik Sep 17 '20 at 06:28