5 Replies - 4533 Views - Last Post: 19 July 2011 - 05:18 AM Rate Topic: ***** 1 Votes

#1 masterjohji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

array of non repeating random numbers

Posted 18 July 2011 - 11:33 PM

this is what I've done so far ... can you tell me where I've gone wrong ,this code is supposed to give me non repeating random numbers from 1~6 , but when I try to run it gives me results like 1-5-5-4-3-2 , 2-3-4-4-5-6...it looks like I got the first two numbers correct because they are always different each time I run it but I'm not very sure tho.

                                  for(int x=0;x<=5;x++)
					{	score[x]=(int)(1+Math.random()*6);
						if(x>=1)
						{	
							int x2 = x-1;
							for(int c=0;c<=x2;c++)
                                                        {
                                                        if(score[c]==score[x])
							score[x]=(int)(1+Math.random()*6);
                                                        }
						}
					}	

This post has been edited by masterjohji: 18 July 2011 - 11:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: array of non repeating random numbers

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

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

Re: array of non repeating random numbers

Posted 18 July 2011 - 11:47 PM

It's because you don't have any conditions checking if the random number already produced is already provided once. What you should do is create another array or preferably an arraylist, then when you encounter a random number, check if it exists on the list of the numbers already existing, if not, add it to the list, if it exists, loop again. I believe you should use a do while loop with this one.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Re: array of non repeating random numbers

Posted 18 July 2011 - 11:51 PM

You have a problem with a loop, if the next time the number generated is the same you dont check for it again, instead you go forward. In your inner for loop, if the number generated matches, just restart the loop to start again. Something like:
if(score[c]==score[x]){
  score[x]=(int)(1+Math.random()*6);
  c =-1;} 

Also you may look about shuffling in java collection:
http://download.orac...ollections.html
http://leepoint.net/...-shuffling.html
http://www.roseindia...rateRandom.html
http://javafaq.nu/java-article719.html
Was This Post Helpful? 1
  • +
  • -

#4 masterjohji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-June 11

Re: array of non repeating random numbers

Posted 19 July 2011 - 01:05 AM

OK thanks guys , I already fixed my mistakes

here's my code now.

                          for(int x=0;x<=5;x++)
                                {
                                    score[x]=(int)(1+Math.random()*6);
                                    if(x>0)
                                        for(int c=0;c<=x-1;c++)
                                            if(score[c]==score[x])
                                                x--;
                                }

Was This Post Helpful? 1
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Re: array of non repeating random numbers

Posted 19 July 2011 - 01:17 AM

Glad you get it working ;)
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,297
  • Joined: 20-September 08

Re: array of non repeating random numbers

Posted 19 July 2011 - 05:18 AM

Quote

this code is supposed to give me non repeating random numbers from 1~6


It would be easier to do:

	Integer[] a = { 1, 2, 3, 4, 5 };
	List<Integer> nums = new ArrayList<Integer>(Arrays.asList(a));
	for(Integer i : a) {
	    Integer score = nums.remove((int)(Math.random() * nums.size()));
	    System.out.println(score);
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1