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 8 of 8

Thread: Randomized array members.

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Randomized array members.

    Hello everyone,

    I'm rather new to Java and not overly experienced with programming in general. I am designing algorithms myself in order to improve, and I'm kind of stuck in a problem I created.

    I have an array of objects with a certain size, say 1000. These objects' constructor takes a value as an argument. I have a variety of values that I can assign, but a certain value can only be assigned so many times in the array. For example I want to fill my array with 800 objects initialized with value a, 20 with value b, 1 with value c etc etc.

    The position of objects in the array is relevant and it is also necessary that they are placed randomly. However, each object I create I want to have a chance of taking a specific value relevant to the number of times that value can yet be applied. Specifically because I want most of my objects to take a single value, I do not want all other values to be crammed either in the beginning (as would happen if I used equal chances until I ran out of the other values) or the end (as would happen if I worked percentage wise). I need everything to be dispersed across the array relatively evenly.

    So, how do I go about it? Any ideas?

    Thanks in advance.


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default Re: Randomized array members.

    Have you actually tried any code for this?

    Can you please post that if available?

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Randomized array members.

    I don't have my project with me, but I'll try and demonstrate my process. I've gone about it in the regular percentage method I described.

    private int array_size;
    private int val1, val2, val3, val4, val5;
    private my_class[] arr;
    private float val1per, val2per, val3per, val4per, val5per;
     
    public class build_array(){
      get_values();
      arr = new my_class[array_size];
      for(int i=0;i<array_size;i++){
        val1_per = val1/array_size;
        val2_per = val2/array_size;
        val3_per = val3/array_size;
        val4_per = val4/array_size;
        val5_per = val5/array_size;
        if(my_random() = val1){
         arr[i] = new my_class(val1);
         val1--;}
        // do for all values
       }
    }
     
    public int my_random(){
      // evenly split 100 between the five value percentages rounded, can't remember what I did here exactly
      // insert some way to get a random number from 1 to 100, again don't remember what I used
      // match that number with a variable and return it
    }
     
     
    public void get_values(){
      // got some file reading done here, but I'll simplify it 
      array_size = 100
      val1 = 70;
      val2 = 18;
      val3 = 1;
      val4 = 4;
      val5 = 7
    }

    But like I said, this hasn't been working very well for me, there's an extremely small chance of getting any other value than val1 (in this example it's still plausible, but with bigger numbers and larger differences it wouldn't happen) in the first members of the array. In other words, though my algorithm does what I intend to more or less, the placement of the values across the array members is uneven.
    Last edited by Dion_sama; December 23rd, 2010 at 03:33 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Randomized array members.

    If all your values in the array are single characters, make a char array.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Randomized array members.

    public class build_array(){

    this will make an error.

    You're both trying to define the beginning of a class and making that beginning into a constructor at the same time.

    Lose the () after array.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Randomized array members.

    arr = new my_class[array_size];

    You never defined array_size. You declared it but never gave it a value.

    That also will make an error.

    If you're allowed to, why not pass the array size to the constructor?

  7. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Randomized array members.

    Quote Originally Posted by javapenguin View Post
    arr = new my_class[array_size];

    You never defined array_size. You declared it but never gave it a value.
    I did, in that function I called.

    Quote Originally Posted by javapenguin View Post
    public class build_array(){

    this will make an error.

    You're both trying to define the beginning of a class and making that beginning into a constructor at the same time.

    Lose the () after array.
    Woops, slipped. As I said I typed this out from memory as I don't have the project with me.

    Quote Originally Posted by javapenguin View Post
    If all your values in the array are single characters, make a char array.
    They're objects of a different class I created, I just didn't type it here.

    I should clarify that my code functions as I explained, just doesn't do what I want it to. I'm looking for a way to evenly distribute different values (that appear with different frequencies) along an array without stacking most of them in either the beginning or the end of the array.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Randomized array members.

    public class build_array
    {
    private int array_size;
    // private int val1, val2, val3, val4, val5;
    private int[] vals;
    private my_class[] arr;
    // private float val1per, val2per, val3per, val4per, val5per;
    private float[] valpers;
    private Scanner inFile;
    public build_array()
    {
    inFile = new Scanner("FileName.extension");
    // I'm assuming array_size is the first value scanned in
    vals= new int[6];
    valper = new float[5];
    int v = 0;
    while (v < 5)
    {
    vals[v] = Integer.parseInt(inFile.nextLine());
    v++;
    }
      // get_values();
      arr = new my_class[vals[0]];
      for(int i=0;i<vals[0];i++){
     
    valper[i] = vals[i+1]/vals[0];
        if(my_random() == vals[1]){
         arr[i] = new my_class(vals[1]);
    int whatever = vals[1];
    whatever = whatever - 1;
    vals[1] = whatever;
         }
        // do for all values
       }
    }
     
    public int my_random(){
      // evenly split 100 between the five value percentages rounded, can't remember what I did here exactly
      // insert some way to get a random number from 1 to 100, again don't remember what I used
      // match that number with a variable and return it
    }
     
    // the method below does nothing that I'm aware of.  
    public void get_values(){
      // got some file reading done here, but I'll simplify it 
      array_size = 100
      val1 = 70;
      val2 = 18;
      val3 = 1;
      val4 = 4;
      val5 = 7
    }

Similar Threads

  1. Hello to all Admins and members of the forum
    By bashiru in forum Member Introductions
    Replies: 1
    Last Post: December 8th, 2010, 06:20 AM
  2. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  3. hello all javaPF's members
    By mary in forum Member Introductions
    Replies: 1
    Last Post: July 3rd, 2010, 03:56 PM
  4. Greetings JPF members
    By dewboy3d in forum Member Introductions
    Replies: 3
    Last Post: May 23rd, 2009, 02:56 AM