5
$\begingroup$

Say I would like to generate a pseudo-random number, which only contains the digits 1, 4, 7 (this is arbitrary).

My first guess would be to create and array "147" and create random numbers in the range [0..2] and use these numbers as an index into this array to concatenate a number in a loop.

I wonder if there is a more "mathematics"-like answer?

$\endgroup$
2
  • $\begingroup$ You're correct. The problem becomes trickier if you include 0 in your choice of digits and it is not allowed to lead. $\endgroup$ Commented Aug 4, 2012 at 10:25
  • 1
    $\begingroup$ This questions seems to confuse the concept of number (which is independent from its representation) with the concept of numeral, i.e. the symbol or name that stands for it. Please specify what you really intend (e.g. a real positive number less than one whose decimal representation contains only a given set of digits) and the desired distribution. Sometimes for this sort of problems the hard part is the question, not the answer! $\endgroup$
    – Stefano M
    Commented Aug 4, 2012 at 19:50

1 Answer 1

7
$\begingroup$

Your proposal appears to make sense but what would be important to ask first is what probability distribution you want on those numbers. The set of numbers with only certain digits is rather peculiar as it is specific to the base-10 number system you choose. It would be interesting to hear what you intend to do with those random numbers!

$\endgroup$
2
  • $\begingroup$ Well, I would like to create "pseudo-random" divisions meant as an excercise for pupils to learn divide the pen-and-paper method. Certain digits "accumulated" to a number are easier to divide "by brain", than are others. The brain works like that. So divisors containing 1, 5, 0, 2 are easier to divide than those containing 7, 9, .. This is my use case. So I think it is not that much about the quality of adherece to the underlying distribution. $\endgroup$
    – JohnDoe
    Commented Aug 4, 2012 at 20:23
  • 1
    $\begingroup$ In that case, you indeed don't care about the distribution and your algorithm makes good sense. $\endgroup$ Commented Aug 4, 2012 at 20:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.