# Thread: Generate random number with specific ciphers

1. Member
Join Date
Mar 2011
Posts
7
Rep Power
0

## Generate random number with specific ciphers

Hi! How can I create a random number (int), with 5 ciphers for example? Thank you!:)

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
What do you mean "with 5 ciphers"?

(Dictionary.com tells me that "cipher" means "zero" and "a secret method of writing" amongst other things.)

A random int with 5 zeros in its decimal representation? Chaining together 5 encryption algorithms so that the output for any input is random? Something else?

3. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Sorry for my english, I meant "cypher" or "digit":

figure
number
digit
cipher
numeral
quantity
cypher ...

For example "45399" (5 cyphers). How to generate a random int with an specific number of cyphers? Thank you again

4. Math.random() produces a random number between [0.0 and 1.0), so you would simply need to translate that range to be from 0 to 99999 (if you want to include things like 00012) or from 10000 to 99999 (if you want no leading zeroes).

Something like... (int)(Math.random() * (upper - lower) + lower)

5. You can also use the Random class.

6. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Aha, ok, but I have also another problem... I don't know the exactly the number of cyphers that the random will have; I mean, in the code that I'm making, when you run the program it ask you how many cyphers do you want your random number to have? This also I don't know how to make it... But I have an idea:

You get the int with the # of cyphers, then convert to string and you can know his length. Now you have his length, and now you create vectors with this length (2 vectors) and get one of them filled with 1....0 and 9....9. But how you use this vectors now?

Thank you very much guys! ;)

7. Originally Posted by Artanis
You get the int with the # of cyphers, then convert to string and you can know his length.
Yes that would probably be simplest way unless some math genius can provide you with an expression.

Now you have his length, and now you create vectors with this length (2 vectors) and get one of them filled with 1....0 and 9....9. But how you use this vectors now?
??? I'm sorry but I don't understand what you are asking.

8. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Sorry, I will put it in the simple way... I wanna make a program that ask you "Do you want make a random number? If yes, how many cyphers should it have?" You enter the # of cyphers and you get the random number, you follow me? :)

Thanks!

9. I understand all that but was has 2 vectors got to do with it?

10. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Forget about it, it was just an idea ;P I thought that maybe creating vectors with length = number of cyphers, filling one of them with the min value and the other one with the max value (one cypher per position in the vector), and try to convert it to the normal number but...I don't know how to do it

11. Well you could use a single array/Vector/List with a length of N. Then generate N single digits random numbers 0-9 and add to the list. Then convert the end result to an int but you would have to check if the first digit was zero.

12. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Yes thats a good idea, but how I convert the list into an "int"?

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
how I convert the list into an "int"?

Use the fact that , eg, 7268=10*(10*(10*7+2)+6)+8. The number 7268 is formed from the list 7, 2, 6, 8 by repeatedly multiplying by 10 and adding the next list item.

-----------------

Consider the 6 digit random numbers. They run from 100000 to 999999. You could start with a random number from zero (inclusive) to 900000 (exclusive) then add 100000.

The general case for other values of "n" is similar. Find the upper limit by multiplying 9 by 10 (n-1) times etc.

14. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
Thank you very much guys!! :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•