Results 1 to 14 of 14
  1. #1
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Generate random number with specific ciphers

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #3
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Wink

    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. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You can also use the Random class.

  6. #6
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Artanis View Post
    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. #8
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

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

  10. #10
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #12
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Yes thats a good idea, but how I convert the list into an "int"?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #14
    Artanis is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2010, 11:24 PM
  2. Replies: 3
    Last Post: 04-16-2009, 02:42 AM
  3. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 09:37 AM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:32 PM

Posting Permissions

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