Answer to Question #21331 in Java | JSP | JSF for shalenee

Question #21331
Write a simple java program that generates 10 non-duplicate random integers within the range from 0 to 20.
1
Expert's answer
2012-12-28T05:16:58-0500
public class main
{
staticint n=10;
staticint[] res=new int[n];

publicstatic void main(String[] args)
{
intcount=0;
Randomr=new Random();
inttmp;
while(count<n)
{
if(!isContains(tmp=r.nextInt(21)))
{
res[count]=tmp;
count++;
}
}

System.out.println(Arrays.toString(res));
}

privatestatic boolean isContains(int el)
{
for(int i = 0; i < res.length; i++)
{
if(el==res[i])
{
returntrue;
}
}

returnfalse;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS