The Artima Developer Community
Sponsored Link

Java Answers Forum
Vectors again

2 replies on 1 page. Most recent reply: Jan 13, 2003 2:24 PM by Martin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Martin

Posts: 3
Nickname: muper
Registered: Jan, 2003

Vectors again Posted: Jan 12, 2003 4:32 PM
Reply to this message Reply
Advertisement
The general problem that I have, is to get random numbers into a [ ] - vector, without using the same numbers twice. I want to get the numbers 1-20 into the vector, but not in the numerical order.
Could anyone please tell me how to do this?
Thankful for answers...


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Vectors again Posted: Jan 13, 2003 12:40 AM
Reply to this message Reply
public class RandomVector{
static private java.util.Set vec = new java.util.LinkedHashSet();
static int[] getRandomVector(int size){
java.util.Random rd = new java.util.Random();

while(vec.size() < size){
int tmp = rd.nextInt(size) + 1;
vec.add(new Integer(tmp));
}

Object o[] = vec.toArray();
int a[] = new int[o.length];

for(int i = 0; i < a.length; i++)
a = ((Integer)o).intValue();

return a;
}
}

class TestRV {
public static void main(String args[]){
int a [] = RandomVector.getRandomVector(20);

for(int i = 0; i < a.length; i++)
System.out.println(a);

}
}

Martin

Posts: 3
Nickname: muper
Registered: Jan, 2003

Re: Vectors again Posted: Jan 13, 2003 2:24 PM
Reply to this message Reply
ok, thanks a lot!

Flat View: This topic has 2 replies on 1 page
Topic: String arrays Previous Topic   Next Topic Topic: calling a method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use