-1

is there any algorithm in ruby on rails to generate random incremented data (especially number) for each day with different probability in each stages, in order to show a sample of line chart?

for example (the set of chart data wanted):

date, incremented random number, phase, probability
1 Jul 2018, 2, beginning, increase sharply
2 Jul 2018, 10, beginning, increase sharply
3 Jul 2018, 23, beginning, increase sharply
4 Jul 2018, 26, middle, increase moderately
5 Jul 2018, 29, middle, increase moderately
6 Jul 2018, 32, middle, increase moderately
7 Jul 2018, 34, end, increase slightly
8 Jul 2018, 35, end, increase slightly
9 Jul 2018, 35, end, increase slightly
10 Jul 2018, 36, end, increase slightly

and the requirement are:
- the number of days is defined.
- the total/sum of random number is also defined. For example, the number from the above data set totally will exactly equal to 262 which is already defined before the random number generated.
- the stages should be distributed evenly on each day.

Many thanks in advance for any suggestion or advice.

Best, Randy

  • 1
    The question in general is prone to be opinionated, rather than showing us some code you want a solution for a very specific problem. [Have a look at this](https://stackoverflow.com/help/dont-ask) – Alex.U Jul 03 '18 at 17:09
  • 2
    I would suggest writing an algorithm yourself and while doing that, if you face any issues, post them here. – Jagdeep Singh Jul 03 '18 at 17:27
  • Yes I agree with you all comment. I have try to build it from scratch and it look very well specific now. That all it left is I need an algorithm described in my comment on @alex unger answer. Many thanks – Randy Putra Jul 03 '18 at 18:55
  • Brute force: loop generating five number from 1 to 16 (or 0 to 20) and break if sum is 20. – iGian Jul 03 '18 at 20:14

1 Answers1

0

I don't think there is a built-in solution. But for the type of problem you're trying to solve I would personally adopt a very functional approach, build a small functional components (functional code) and them merge them all (imperative shell).

Although, it it's only for test data you could hard-code values on a range with the only constraint the sum is, for the example above, 262.

Alex.U
  • 1,631
  • 2
  • 16
  • 26
  • I have done it as you suggest. Now its turn-up for me to find the algorithm to generate random numbers with specific interval and specific sum. For example, I need 5 random number with 20 if it all that 5 random number summed up. Many thanks any advice. – Randy Putra Jul 03 '18 at 18:45
  • @RandyPutra You could build a decrement function that goes like: starts at [20] -> generate random between [1..5] -> decrement counter with random number, repeat. Split this across as many steps as phases you need (beginning, middle, end ...) and you should be good to go! – Alex.U Jul 03 '18 at 18:51
  • I've got it. Just applying this excel algorithm https://stackoverflow.com/questions/21782329/generate-n-random-numbers-whose-sum-is-a-constant-k-excel. Many thanks. – Randy Putra Jul 03 '18 at 23:05