# How to assure that a number only contains a certain range of digits?

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?

• You're correct. The problem becomes trickier if you include 0 in your choice of digits and it is not allowed to lead. Aug 4 '12 at 10:25
• 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! Aug 4 '12 at 19:50