Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Need help with Java: generating 6 non-duplicated random num.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
zengrz
Cheater
Reputation: 0

Joined: 19 Mar 2009
Posts: 33
Location: ca

PostPosted: Tue May 19, 2009 7:07 pm    Post subject: Need help with Java: generating 6 non-duplicated random num. Reply with quote

Hi, the 6 numbers cannot repeat itself. I have put them in an array. What I have in mind is to test if the next random number generated matches anyone of the elements in the array. If no, I will add it; if yes, drop it. I am unsure about the syntax. Sorry I am a beginner.

Code:


/** This code is able to generate 6 random numbers, but they may not be
 * different.
 */

import acm.program.*;
import acm.util.*;

public class Random40 extends ConsoleProgram {

   private int[] random = new int[6];
   private RandomGenerator rgen = RandomGenerator.getInstance();
   
   public void run() {
      generateRandom();
      println(random[0] + " " + random[1] +
            " " + random[2] + " " + random[3] +
               " "+ random[4]  + " " + random[5]);
   }
   
   private void generateRandom() {
      int i = 0;
      for (i = 0; i <= 5; i++) {
         random[i] = rgen.nextInt(0, 41);
         pause(50);
      }   
   }

}



Thanks in advance!

(I am using acm library)

* Sorry forgot to say the numbers must be between 0 to 41. Thanks SoccaBallDude for noticing that. Very Happy


Last edited by zengrz on Tue May 19, 2009 8:06 pm; edited 1 time in total
Back to top
View user's profile Send private message
SoccaBallDude
Master Cheater
Reputation: 1

Joined: 29 Aug 2008
Posts: 263

PostPosted: Tue May 19, 2009 7:59 pm    Post subject: Reply with quote

Quickly thrown together. Can definitely be improved.
Remember that the program will loop forever if you set the random number possibilities below the array size.

Code:

import java.util.Random;

public class rand
{
   
   private static int[] random = new int[6];
      private static Random rand = new Random();
      private static int i;
   
   public static void main(String[] args)
   {   
      for (i=0; i<6; i++) generateRandom();
      System.out.println(random[0] + " " + random[1] +
            " " + random[2] + " " + random[3] +
               " "+ random[4]  + " " + random[5]);
   }
   
   private static void generateRandom()
   {
      int temp = rand.nextInt(41);
      boolean check = false;
      for (int c=0; c<random.length; c++)
      {
         if (random[c] == temp)
         {
            check = true;
            System.out.println(random[c]+" at "+i);
            generateRandom();
         }
      }
      if (!check) random[i] = temp;
   }

}
Back to top
View user's profile Send private message MSN Messenger
zengrz
Cheater
Reputation: 0

Joined: 19 Mar 2009
Posts: 33
Location: ca

PostPosted: Tue May 19, 2009 9:11 pm    Post subject: Reply with quote

Quote:


private static void generateRandom()
{
int temp = rand.nextInt(41);
boolean check = false;
for (int c=0; c<random.length; c++)
{
if (random[c] == temp)
{
check = true;
System.out.println(random[c]+" at "+i);
generateRandom();
}
}
if (!check) random[i] = temp;
}



Thanks for the quick reply. That is a very cool recursive method. The program works perfectly!

Can you replace the generateRandom(); in the method by rand.nextInt(41); ? Or there is a possibility that they will generate similar number again on the second try?

Is the "for loop" in that method checking for similar numbers from the first to the "random.length"th number every time a new random number is generated?

What does "if (!check)" mean? Is it the same if you replace it as
"if (check == true)" ?

Is there a non-recursive method to solve this? Sorry if that bothers you.

Thanks for the great work!
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed May 20, 2009 1:19 am    Post subject: Reply with quote

zengrz wrote:

Thanks for the quick reply. That is a very cool recursive method. The program works perfectly!

Can you replace the generateRandom(); in the method by rand.nextInt(41); ? Or there is a possibility that they will generate similar number again on the second try?

Is the "for loop" in that method checking for similar numbers from the first to the "random.length"th number every time a new random number is generated?

What does "if (!check)" mean? Is it the same if you replace it as
"if (check == true)" ?

Is there a non-recursive method to solve this? Sorry if that bothers you.

Thanks for the great work!

if(!check) : the ! means NOT. So it's equal to: if( check != true ) and also equal to if( check == false )

I don't know java, but a non-recursive method would be something like this i think:

Code:

boolean check;
for( int i = 0; i < 5; i++ ){
   while( 1 ){
      int newrandom = rand.nextInt(41);
      check = false;
      for( int j = 0; j < 5; j++ ){
         if( random[j] == newrandom ){
            check = true;
            break;
         }
      }
      if( check == false ){
         random[i] = newrandom;
         break;
      }
   }
}
Back to top
View user's profile Send private message
SoccaBallDude
Master Cheater
Reputation: 1

Joined: 29 Aug 2008
Posts: 263

PostPosted: Wed May 20, 2009 2:58 pm    Post subject: Reply with quote

Improved it just a bit.

Code:

import java.util.Random;

public class rand
{
   
   private static int[] random = new int[6];
      private static Random rand = new Random();
   
   public static void main(String[] args)
   {   
      generateRandom(41);
      System.out.println(random[0] + " " + random[1] +
            " " + random[2] + " " + random[3] +
               " "+ random[4]  + " " + random[5]);
   }
   
   private static void generateRandom(int max)
   {
      for (int a=0; a<6; a++)
      {
         int temp = rand.nextInt(max);
         boolean check = false;
         for (int b=0; b<random.length; b++)
         {
            if (random[b] == temp)
            {
               check = true;
               System.out.println(random[b]+" at "+a);
            }
         }
         if (!check) random[a] = temp;
         else a -= 1;
      }
   }

}
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed May 20, 2009 3:23 pm    Post subject: Reply with quote

Significantly Improved:

Code:
public class RandNums {

    public static boolean numberExistsInArray(int number, int[] array, int len) {
        for (int i = 0; i < len; i++) {
            if (array[i] == number) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Random  rd = new Random();
        int[]   rands = new int[6];

        for (int i = 0; i < rands.length; i++) {
            do {
                rands[i] = rd.nextInt(42);
            } while (numberExistsInArray(rands[i], rands, i));
            System.out.print(rands[i] + " ");
        }
    }
}

_________________
Back to top
View user's profile Send private message
zengrz
Cheater
Reputation: 0

Joined: 19 Mar 2009
Posts: 33
Location: ca

PostPosted: Thu May 21, 2009 10:55 pm    Post subject: Reply with quote

Thanks for all the great replies! Very Happy

I have learn many new ways of solving this problem now. You guys are great!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites