5

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?

Christian Clason
  • 12,301
  • 3
  • 48
  • 68
JohnDoe
  • 201
  • 1
  • 7
  • You're correct. The problem becomes trickier if you include 0 in your choice of digits and it is not allowed to lead. – Aron Ahmadia Aug 04 '12 at 10:25
  • 1
    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! – Stefano M Aug 04 '12 at 19:50

1 Answers1

7

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!

Wolfgang Bangerth
  • 55,373
  • 59
  • 119
  • 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. – JohnDoe Aug 04 '12 at 20:23
  • 1
    In that case, you indeed don't care about the distribution and your algorithm makes good sense. – Wolfgang Bangerth Aug 04 '12 at 20:27