All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 9 random digits (Posted on 2003-05-02)
Suppose you want to make a random 9 digit number, using every number from 1 to 9 exactly once. You have a process called random(top) that gives a random number up to top (if top was 5, it would give random numbers from 1 to 5)

a) How could you do this?

b) If top couldn't be more than 9, how could you do this using random(top) only 9 times (or less)?

 See The Solution Submitted by Gamer Rating: 3.4000 (5 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 Words too! | Comment 6 of 20 |
(In reply to C++ solution by friedlinguini)

Can you explain it in words too? I think that's half the problem.

I think Charlie had my idea.

One other way you could do it is:

(all variables start out as 0)
[
Mutliply x by 10
Add one to the variable n
Do (rand(9) to get x. ) while the first n digits of answerlist aren't equal to x.
] Do all that is in the [] 9 times.

This is an inefficient way to accomplish this, but it is another way. It is also short code-wise.

 Posted by Gamer on 2003-05-02 10:46:14

 Search: Search body:
Forums (0)