0

Can somebody suggest me a random (integer) number generation algorithm such that given an interval, it is absolutely guaranteed to cover all the given interval (i.e. o numbers are missing)? No other constraints, e.g. no uniqueness of numbers, no hypotheses on distribution etc...

The idea is the following:

myRandom(1, 4)

Outputs a sequence like:

1, 4, 3, 3, 1, 3, 1, 4, 1, 3, 2

1 Answer 1

1

You somehow have to tell your function how many elements you want, say n. If that is known (or you have some reasonable default) you can use:

  • create sequence from your input values
  • select with replacement n - sequence_length values from your sequence
  • concatenate the sequence with the selected values
  • shuffle the result

Sample code in R using 4 * (to - from) as default value for n:

myRandom <- function(from, to, n = 4 * (to - from)) {
    values <- seq.int(from, to)
    selection <- sample(values, n - length(values), replace = TRUE)
    values <- c(values, selection)
    sample(values)
}

myRandom(1, 4)
#>  [1] 1 4 3 3 3 4 4 2 3 2 1 2

Created on 2019-07-09 by the reprex package (v0.3.0)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.