Results 1 to 13 of 13
  1. #1
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default Need help generating random numbers atleast 2 characters apart

    I am trying to generate two random numbers that are apart by atleast 2 numbers, in order to create a random substring of atleast two characters.. I have spent four hours trying to figure it out to no avail.. help please!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello, and welcome to our forum! Let's see what you have so far so we can see what you may be doing right and may be doing wrong. Please don't forget to use code tags when posting code (please see my signature after this post in blue text). Much luck!

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Have you looked at the Math.random() function on the API?

    you can also have a look at this site
    Java: Random numbers - API
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  4. #4
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the heads up, I did not want to post my code, because I ws not sure about the tags... here is what I have for the random part.. the rest is long so I don't want to post the whole thing.

    Java Code:
    	Random random = new Random (seed);
    	int start = 0, end = strng.length();
    	int random1 = random.nextInt( end - start + 1) + start;
    	int random2 = random.nextInt( end - start + 1) + start;
    	int min = Math.min(random1,random2);
    	int max = Math.max( random1,random2 );
    The random numbers are generating correctly, but I need them to return a minimum of 2 characters in the substring, but somewimes it returns the same number twice such as (1,1) or one number apart such as (0,1).
    Thanks

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Or are you Stuck at the "atleast 2 characters apart" ?
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  6. #6
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi Dieter, I did see that and many other help sites. I guess I am just not sure how to incorporate any of those methods into the Random function to make them atleast 2 apart.

    Thanks for the reply.

  7. #7
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Ok my bad for not understanding you, i'm off to school now... will try to look at it there :p
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  8. #8
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    i think i got someting simular on a test. but the project is on the computer in my school. will post some code around 9.00

    Hopefully you will have an answer by then...

    sorry i can't help you instant

    Best of luck,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  9. #9
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help.. been going crazy for the last four hours trying all different methods that do no work....

  10. #10
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Can you post a code you tried?
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  11. #11
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    Random random = new Random (seed);
    	int start = 0, end = strng.length();
    	int random1 = random.nextInt( end - start + 1) + start;
    	int random2 = random.nextInt( end - start + 1) + start;
    	int min = Math.min(random1,random2);
    	int max = Math.max( random1,random2 );
    
    System.out.print("The string between " + random1 + "and" + random2 + "is: ");

  12. #12
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    I donno if this is what you need but i got someting that might help you.

    This example shows you the numbers between -10 and -5...

    Java Code:
    int [] generate = new int[50];
    
    for (int i = 0; i < generate.length; i++) {
                    generate[i] = (int)(Math.random() * 5) - 15;
                    System.out.println(generate[i]);
    }
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  13. #13
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    you can play with this kind to work your way to yours.
    else i didn't get your question

    all the best,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

Similar Threads

  1. problems generating random objects
    By mandangalo in forum New To Java
    Replies: 8
    Last Post: 07-30-2010, 04:42 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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