11 Replies - 1940 Views - Last Post: 12 October 2011 - 04:56 PM Rate Topic: -----

#1 darcmasta  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 27-September 11

Print out random array value with no repeats until end

Posted 11 October 2011 - 11:10 PM

Hey everyone I am back! Just finished a coding assignment, but I was wondering how to output an array of integers of size 10. Randomly output a sub, and have that value not come up until the end?

int[] numbers = fillArrayNumbers();

      for(int i=0; i<numbers.length; i++)
      {
        int randomNumber = (int) (Math.random() * 10);
        (numbers[randomNumber]);
      }



Here is the basic code, I am think something along the lines of another array of type boolean. Monitoring each sub for either true or false. And if the field is false print it out, but then set that sub to true. If it falls on a sub that is true, randomly generate another sub slot instead.

Is my logic on the right path? Thanks for any help, and examples you may provide!

Is This A Good Question/Topic? 0
  • +

Replies To: Print out random array value with no repeats until end

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Print out random array value with no repeats until end

Posted 11 October 2011 - 11:21 PM

Can you give us a more concrete example of what you are trying to achieve?
Was This Post Helpful? 0
  • +
  • -

#3 darcmasta  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 27-September 11

Re: Print out random array value with no repeats until end

Posted 11 October 2011 - 11:35 PM

So basically I have an array with 10numbers. Let's say 1-10 to make it simple. I can print out a specific sub of the array just fine randomly, but what I am trying to do is basically not print out repeating numbers until the array runs completly. So

Array[0] printed out
array[7] printed out
Array[0] tries to get printed out but its been printed out before so get another random sub
array[7] agaiin same as above
array[5] printed out
...
...

Hope this helps out. Like I said I think imma need a boolean array to do validation not sure though if there is an easier way.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Print out random array value with no repeats until end

Posted 11 October 2011 - 11:42 PM

One Idea, you can use arryList/list of your numbers, then generate random number from 0 to the size of list, use it as index and print the number, then remove the already printed number from list. This will make sure you are not going to loop to infinity by waiting to the desired occurrence from random selection
Was This Post Helpful? 0
  • +
  • -

#5 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 12:07 AM

checked DIC snippets? there should be one for this.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 03:51 AM

http://www.dreaminco...snippet2992.htm

can be easily adapted to your problem
Was This Post Helpful? 1
  • +
  • -

#7 darcmasta  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 27-September 11

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 12:10 PM

Is there anyway to do this, without an arraylist? Cant I do something like

array1 string[10]
array2 int[10]
array3 boolean[10]
for(i=0; i<array2.length; i++)
  int randomNumber = (int) (Math.Random()*10)
  if(array3[randomnumber]==true)
    set JTextArea equal to array1[randomNumber];
  else
    get another random number and run loop again


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 12:13 PM

Parallel arrays really aren't good practice. You should avoid them. The easiest way to do this is with an ArrayList, or better yet a Set. Otherwise, you have to add a lot more to deal with arrays.
Was This Post Helpful? 1
  • +
  • -

#9 darcmasta  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 27-September 11

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 12:30 PM

View Postmacosxnerd101, on 12 October 2011 - 12:13 PM, said:

Parallel arrays really aren't good practice. You should avoid them. The easiest way to do this is with an ArrayList, or better yet a Set. Otherwise, you have to add a lot more to deal with arrays.


Yeah, this isn't really required for my assignment. The teacher posed this to us as extra credit, and the way he explained it was basically the way I wrote it above. I don't really want to jump out of his explanation, even though it may be wrong. As is my program below works just fine, and its what he was looking for. Thanks everyone, if anyone knows how to do this without a arraylist feel free to post.

    public void actionPerformed(ActionEvent e)
    {
	  //generates a random quote from the array of twainQuotes
	  if(e.getSource() == twainB)
	  {
        String[] twainQuotes = fillArrayFromTwain();

        for(int i=0; i<twainQuotes.length; i++)
        {
		  int randomNumber = (int) (Math.random() * 10);
          quotesTA.setText(twainQuotes[randomNumber]);
        }
      }


This post has been edited by darcmasta: 12 October 2011 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 01:58 PM

You can do it with 2 arrays it will work
Was This Post Helpful? 0
  • +
  • -

#11 darcmasta  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 27-September 11

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 04:15 PM

View Postpbl, on 12 October 2011 - 01:58 PM, said:

You can do it with 2 arrays it will work


So something like set all values in the Boolean array to true, make the loop as i did. If the spot equals true make text area equal quote, also make that sub in the bool array false. else get another random number and try again?

I am just trying to figure out the logic, Sorry to be such a bother with what seems to be an unconventional form of programming. =/
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Print out random array value with no repeats until end

Posted 12 October 2011 - 04:56 PM

Actually you need only one array :)
Randomly select from it, then move the last element to the one you just used,


  String[] quotes = {"Hello", "AnotherOne", "Good Luck", "That's it"};
  int arraySize = quotes.length;
  Random ran = new Random();

  public void actionPerformed(ActionEvent e) {
      int index = ran.nextInt(arraySize);
      ... display quotes[index];
      quotes[index] = quotes[--arraySize];
  }


Happy coding
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1