Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: problem with Random()

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default problem with Random()

    Hello.

    Iam having a problem with my code that makes the Random() part.

    This is a code for a memory-game in android, but this question is more pure java than android sdk.

    This is the code that i run right now.

    ROW_COUNT = 3 and COL_COUNT = 4 
    cards = new int [COL_COUNT] [ROW_COUNT];

    private void loadCards(){ 
                    try{ 
                    int size = ROW_COUNT*COL_COUNT; 
     
     
                    ArrayList<Integer> list = new ArrayList<Integer>(); 
     
     
                    for(int i=0;i<size;i++){ 
                            list.add(new Integer(i)); 
                    } 
     
     
                    Random r = new Random(); 
     
     
                    for(int i=size-1;i>=0;i--){ 
                            int t=0; 
     
     
                            if(i>0){ 
                                    t = r.nextInt(i); 
                            } 
     
     
                            t=list.remove(t).intValue(); 
                            cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2); 
                    } 
                } 
                    catch (Exception e) { 
                            Log.e("loadCards()", e+""); 
                    } 
     
     
        }

    The problem is that this will always make cards[0][0] = 5 becourse iam not requesting a random
    number when i==0

    if i try to change that

     if(i>0){ 
                                   t = r.nextInt(i); 
                           }
    to
     if(i>=0){ 
                                   t = r.nextInt(i); 
                           }

    This is wrong of course: r.nextInt(0) will throw IllegalArgumentException.

    So my question is to you guys if there is a way round this problem of mine?

    as you could see, iam new to this so please be gentle


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: problem with Random()

    Are you trying to make it so that the number is likely to get bigger the further you go?

    All you have to do is make t = r.nextInt(i+1), so when i=0 it does r.nextInt(1) [Always 0!]

    Are you trying to make each a random number?

    Choose a max number and always call it like so: r.nextInt(100) [A psuedorandom number between 0 and 99]

    Are you trying to make a set of unique values in a random order?

    Look into the Collections.shuffle(List<?> list) method, although it would be a bit more complicated because it is a 2d array, but in effect you shuffle each deck in the array with Collections.shuffle(Arrays.asList(cards[i])) where i is a incrementing counter, then you shuffle Arrays.asList(cards) and bam, you got it.

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    Isaksson (March 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with Random()

    Hello.

    Iam trying to make it load the numbers two times like this in the logg:

    03-23 21:42:38.519: INFO/loadCards()(359): size=12

    03-23 21:42:38.529: INFO/loadCards()(359): card[3][2]=4

    03-23 21:42:38.529: INFO/loadCards()(359): card[2][2]=0

    03-23 21:42:38.529: INFO/loadCards()(359): card[1][2]=1

    03-23 21:42:38.529: INFO/loadCards()(359): card[0][2]=4

    03-23 21:42:38.529: INFO/loadCards()(359): card[3][1]=5

    03-23 21:42:38.529: INFO/loadCards()(359): card[2][1]=5

    03-23 21:42:38.529: INFO/loadCards()(359): card[1][1]=2

    03-23 21:42:38.529: INFO/loadCards()(359): card[0][1]=1

    03-23 21:42:38.529: INFO/loadCards()(359): card[3][0]=2

    03-23 21:42:38.539: INFO/loadCards()(359): card[2][0]=3

    03-23 21:42:38.539: INFO/loadCards()(359): card[1][0]=0

    03-23 21:42:38.539: INFO/loadCards()(359): card[0][0]=3

    This is the logg when i changed the code to t = r.nextInt(i+1) and it seems that it did the trick, iam going to test this some more but it seems good right now.

    Thanks for the help.

  5. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: problem with Random()

    It appears you want a set of psuedorandom numbers, in which it would be better to have that line something closer to

    t = r.nextInt(6);

    Which would be a random number between 0 and 5.

Similar Threads

  1. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  2. [SOLVED] Random Colors Problem
    By captain in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 23rd, 2012, 10:40 PM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  4. random problem
    By kantaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2011, 08:00 AM
  5. Random Letter problem please help!
    By xs4rdx in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2010, 12:27 PM