Answer the question
In order to leave comments, you need to log in
How to generate random non-overlapping ranges in php?
Good
afternoon Tell me, can anyone have the code already ready?
How to make a random range generator in PHP?
At the input we have
- the length of the entire range (1) - from 100 to 1000 for example
- the length of the required subranges (2) - 10 for example
- the total number of ranges (3) - 3 for example
Everything rests on the overlap
The algorithm is so simple
From the initial range of numbers we generate random numbers based on (1)
For example, 400, 500, 900
And we get the ranges 400-410, 500-510, etc.
Everything is elementary here
But how to make these ranges not overlap In this case
, for example, we have (1) from 0 to 15, (2) equal to 3, (3) equal to 4
There is a high probability that the ranges overlap each other .
First I tried it like this
. Sort the numbers in ascending order. Looked for the next one to
be
greater than the previous one + the length of the range
. "
That is, they similarly need to be somehow shifted to the left (to the beginning of the general range)
In general, please tell me, or what algorithm or can the code already be ready
? I rummaged on the Internet, like nothing ready or close
Thank you all)
Answer the question
In order to leave comments, you need to log in
Operations on sets:
1. inside range -> exclude
2. partial overlap -> add missing
3. no overlap -> add
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question