# Generate random number with specific ciphers

• 03-23-2011, 07:20 PM
Artanis
Generate random number with specific ciphers
Hi! How can I create a random number (int), with 5 ciphers for example? Thank you!:)
• 03-23-2011, 08:37 PM
pbrockway2
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?
• 03-23-2011, 10:10 PM
Artanis
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
• 03-23-2011, 11:10 PM
Zack
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)
• 03-23-2011, 11:22 PM
Junky
You can also use the Random class.
• 03-23-2011, 11:40 PM
Artanis
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! ;)
• 03-23-2011, 11:44 PM
Junky
Quote:

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.

Quote:

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.
• 03-24-2011, 01:02 AM
Artanis
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!
• 03-24-2011, 01:12 AM
Junky
I understand all that but was has 2 vectors got to do with it?
• 03-24-2011, 01:30 AM
Artanis
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
• 03-24-2011, 01:39 AM
Junky
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.
• 03-24-2011, 01:22 PM
Artanis
Yes thats a good idea, but how I convert the list into an "int"?
• 03-24-2011, 06:41 PM
pbrockway2
Quote:

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.
• 03-25-2011, 01:34 AM
Artanis
Thank you very much guys!! :)