Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
lamentable dustman
Apr 13, 2007

🏆🏆🏆

Trying to generate some images in Java. To visualize what the grid would look like:

code:
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
100 100 100 200 200 200 300 300 300 300 
The numbers are the z-axis and will be represented by different colors. I need to draw this and save to a png or jpeg file. Not sure how to go about it, I'm playing around with the AWT which I've never used before, any tips?

I would eventually like to make it 3D for an isometric view but that isn't required and down the road.

Adbot
ADBOT LOVES YOU

zootm
Aug 8, 2006

We used to be better friends.
You probably just want to draw into a BufferedImage and use ImageIO.write(RenderedImage,String,String) to write it out.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Yea, that looks like more what I need to do. Didn't occur to me that I could do that. I've only used BufferedImages to modify existing images, never to create one from scratch.

Mill Town
Apr 17, 2006

dvinnen posted:

Yea, that looks like more what I need to do. Didn't occur to me that I could do that. I've only used BufferedImages to modify existing images, never to create one from scratch.

Yeah, it's quite easy to create a new BufferedImage and just loop through all the pixels, filling them in with what you want. Something like this:

code:
BufferedImage im = new BufferedImage( IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB );
for(int i=0; i < IMAGE_WIDTH; i++){
    for(int j=0; j < IMAGE_HEIGHT; j++){
        int r, g, b;
        //Do your calculations here that fills in the RGB values in r, g, and b
        im.setRGB(i, j, r<<16 + g<<8 + b );
    }
}
I'm actually doing this in one of my servlets, it generates images from data programmatically and serves them up at a URL, it never even saves them anywhere.

zootm
Aug 8, 2006

We used to be better friends.
Yeah, "BufferedImage" is just "in memory image". It's your go-to class for drawing stuff from what I've seen.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Mill Town posted:

Yeah, it's quite easy to create a new BufferedImage and just loop through all the pixels, filling them in with what you want. Something like this:

code:
BufferedImage im = new BufferedImage( IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB );
for(int i=0; i < IMAGE_WIDTH; i++){
    for(int j=0; j < IMAGE_HEIGHT; j++){
        int r, g, b;
        //Do your calculations here that fills in the RGB values in r, g, and b
        im.setRGB(i, j, r<<16 + g<<8 + b );
    }
}
I'm actually doing this in one of my servlets, it generates images from data programmatically and serves them up at a URL, it never even saves them anywhere.

Sweet, thanks man, looks like that is exactly what I needed to see. It looks like the line:

code:
im.setRGB(i, j, r<<16 + g<<8 + b ); 
should be:


code:
im.setRGB(i, j, r<<16 | g<<8 | b ); 
encase anyone is trying to run the code. Also tack on the end to write to a file:

code:
try {
			ImageIO.write(im, "jpeg", new File("c:/imageTest.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.
I'm trying to create an arrayList of an object. The object is composed of three strings, and some simple methods.

Constructor for my Calc object.
code:
public class Calc 
{

    private String Name;
    private String Quantity;
    private String Units;

     public Calc(String CalcName, String CalcQuantity, String CalcUnits)
     {
         Name = CalcName;
         Quantity = CalcQuantity;
         Units = CalcUnits;
     }

//some return methods
If I create an arrayList of Calcs in main, the compiler will let me feed Calcs in, but all I get back when I use arrayList.get(index) are memory locations.

What I basically have now:
code:
       
        int arrayCount = 0; 
        Calc MyCalc = null;
        ArrayList al = new ArrayList();
//loop
       al.add(arrayCount, MyCalc);
       System.out.println("This is the Calc Object: " + al.get(arrayCount));  

//Output is similar: This is the Calc Object: Calc@1fb8ee3

I think I need to create a new arrayList class specific to my object, but I'm not sure how to do it such that I can retrieve each string individually.

oh no computer
May 27, 2003

Lazlo posted:

When you print a Calc object, System.out.println calls its toString() method. Since you haven't defined one yourself, it calls the toString method from Object (which all java objects inherit), which (I believe) is the hash code of that object.

Basically if you want to be able to print the object in a way that makes sense, you need to define your own toString() method in Calc:

public String toString()
{ return name; }

or whatever best suits your needs.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Lazlo posted:

I'm trying to create an arrayList of an object. The object is composed of three strings, and some simple methods.

Constructor for my Calc object.
code:
public class Calc 
{

    private String Name;
    private String Quantity;
    private String Units;
...
}
If I create an arrayList of Calcs in main, the compiler will let me feed Calcs in, but all I get back when I use arrayList.get(index) are memory locations.

System.out.println("This is the Calc Object: " + al.get(arrayCount));

//Output is similar: This is the Calc Object: Calc@1fb8ee3


You need a toString() method in your Calc class, then when you print it out it should be:
System.out.println("This is the Calc Object: " + al.get(arrayCount).toString()); // Don't actually need the .toString() part - it's automatically called.

Something vaguely like this:

code:
public String toString() {
  String output = "Calc[Name: " + Name +
      ", Quantity: " + Quantity + ", "Units: " + Units + "]";
  return output;
}

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.

Kilson posted:



Cool, thanks. But what if I also want to retrieve the members individually, like "Units" and "Quantity" from a particular index, is there a way I can do that with a method in the Calc class?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
First of all you should be using generics, it makes your code much easier to work with since you don't have to cast your objects all the time. This has some other benefits like preventing accidentally adding an object that isn't a Calc into your arraylist.

code:
ArrayList<Calc> calcs = new ArrayList<Calc>();
Second you need to make getter and setter methods for each of those fields if you want to be able to modify them.

code:
public class Calc { 
private String Name; 
private String Quantity; 
private String Units;

    public String getQuantity(){
        return Quantity;
    }

    public void setQuantity(String Quantity){
        this.Quantity = Quantity;
    }
    ....
}
Then you can do things with those objects in your for loop.

Janitor Prime fucked around with this message at 04:55 on Oct 28, 2008

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
quote != edit

Startacus
May 25, 2007
I am Startacus.
Can someone please explain lazy deletion to me? I know you just mark something as being deleted rather then actually deleting it but I'm afraid I'm not quite sure what that means.



Thanks

lament.cfg
Dec 28, 2006

we have such posts
to show you




.

lament.cfg fucked around with this message at 00:55 on Jan 15, 2011

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Startacus posted:

Can someone please explain lazy deletion to me? I know you just mark something as being deleted rather then actually deleting it but I'm afraid I'm not quite sure what that means.

In data structures, it usually means that you leave a hole in your structure (typically marked in some fashion, maybe using null as a sentinel) instead of actually cleaning up after yourself. In situations where deletion is rare, that can be a major win, at least in terms of development effort.

traveling midget posted:

But I'm basically oblivious as to what the Unit class should look like. DAYS should be a Unit object, but I have no clue how to represent them.

There's an efficient solution which exploits the linear relationship between these units, and there's a less-efficient solution which addresses the general problem that Unit.DAYS and Unit.MINUTES might need to behave differently. If I've got two objects, and they need to behave differently when a particular method is called on them, what's the general way to achieve that?

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
I'm playing around and messing with some programming homework and I'm getting a little stumped on this. This program basically uses a linked list and lets the user write a polynomial, input a number to be evaluated, and then returns the answer. So far I have all the math working, except I can't figure out a solution to print out the user's polynomial. Here is my code:
Polynomial.java
Node.java

When I compile this code, I am getting this output:
code:
Please enter the coefficient first and then the exponent.
When you are done, type 0 for the exponent.
Please enter the numbers you want:
3
2
Please enter the numbers you want:
4
5
4x^5 + 
Please enter the numbers you want:
6
7
6x^7 + 
6x^7 + 
Please enter the numbers you want:
5
2
5x^2 + 
5x^2 + 
5x^2 + 
Please enter the numbers you want:
4
0
4x^0 + 
4x^0 + 
4x^0 + 
4x^0 + 
Please enter the value of x you want to evaluate.
6
f(6) = 1711012

As you can see, It is outputting each the polynomial node after the input, and it just prints the same node each time. What is the best way to go about this? Also, for some reason it doesn't print out the first inputs, and starts printing them after you input the second numbers. Why is it doing that? any help would be appreciated.

lmao zebong fucked around with this message at 03:22 on Oct 31, 2008

ynef
Jun 12, 2002

Sarah Sherman posted:

I'm playing around and messing with some programming homework and I'm getting a little stumped on this. This program basically uses a linked list and lets the user write a polynomial, input a number to be evaluated, and then returns the answer. So far I have all the math working, except I can't figure out a solution to print out the user's polynomial. Here is my code:
Polynomial.java
Node.java
As you can see, It is outputting each the polynomial node after the input, and it just prints the same node each time. What is the best way to go about this? Also, for some reason it doesn't print out the first inputs, and starts printing them after you input the second numbers. Why is it doing that? any help would be appreciated.

You will be really annoyed when you find out the answer.

From where are you getting the values you are printing?

lmao zebong
Nov 25, 2006

NBA All-Injury First Team

ynef posted:

You will be really annoyed when you find out the answer.

From where are you getting the values you are printing?

Ah poo poo, thanks for the nudge in the right direction. Now I need to figure out a way to have it not print the polynomial until after the user is done inputting the numbers...
if I try to do this:
code:
        while(iValue != 0)
	{
		System.out.println("Please enter the numbers you want:");
		aValue = scanner.nextInt();
		iValue = scanner.nextInt();
		counter++;
		//function.printPoly();
		function.appendPoly(aValue, iValue);
							
	}

	for(int i = 0; i <= counter; i++)
	{
		function.printPoly();
	}
it is giving me outputs like this:
code:
3x^2 + 
4x^6 + 
5x^4 + 
3x^2 + 
4x^6 + 
5x^4 + 
3x^2 + 
4x^6 + 
5x^4 + 
3x^2 + 
why is it giving me each node from the list multiple times?
also, thanks a lot for the help.

the onion wizard
Apr 14, 2004

I'm assuming printPoly() prints all of the Polynomial's nodes each time it is called? How many times is printPoly() being called in your example?

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
drat, now i'm thinking you guys know my code better than i do. thanks a lot for the help guys, it's always nice to have a couple fresh sets of eyes to catch the glaring errors in your code that you overlook.

Wyznewski
Oct 17, 2005
DJCOMMIE, CAN YOU PLEASE CONTACT ME VIA AIM? THANKS! -SIGTRAP
(I don't know how to contact you so I'll just use this space)
I have a question about JUnit, specifically the (now deprecated) swingui test runner. We've got an old version of JUnit (3.8.1) specifically because the teacher wanted the green bar for all testing, but we're having trouble getting it working. The window opens, but only while the tests are running. Does anyone know how to make it leave the window open after it's done? We're using Ant 1.7.1, I can post build.xml or anything else that's relevant. Thanks!

[Fake edit] IDEs are verboten for this project. We're running everything from a command line.

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.
I need a way to search for a value through a hierarchical list, while keeping track of each descent made. The hierarchy may have an arbitrary number of levels (nodes), though probably less than ten.

I think I can come up with a way to parse each line up using a Scanner, but I cant think of a good way to keep track of where Ive been in the hierarchy. Is there a package I could use to make navigating through the list easier?

Example of the input I want to process:
code:
    (1.1)HAS SDE CONTEXT:Subject Class(LNX,13254)[TAG] = Person(LNX,121025)
    (1.2)HAS SDE CONTEXT:Subject Name(LNX,134945)[PNAME] = NAME
    (1.3)CONTAINS:Person Characteristics(LNX,1435934)[CONTAINER] = {SEPARATE}
        (1.3.1)CONTAINS:SubCatagory1(LN,189316-6)[NUM] = 3 no units
        (1.3.2)CONTAINS:SubCatagory2(LN,13177-6)[NUM] = 2 no units
    (1.4)CONTAINS:Summary(LNX,1216511)[CONTAINER] = {SEPARATE}
        (1.4.1)CONTAINS:CDF(LN,15255-2)[DATE] = 20070524
        (1.4.2)CONTAINS:Con Summary(LNX,134568)[CONTAINER] = {SEPARATE}
            (1.4.2.1)HAS SDE CONTEXT:Con ID(LN,4321-1)[TEXT] = A
            (1.4.2.2)CONTAINS:Con X(LN,457621-1)[NUM] = 151 day
            (1.4.2.3)CONTAINS:Composite Age(LN134346-5)[NUM] = 133 day
            (1.4.2.4)CONTAINS:LGT rank(LN,123647-1)[NUM] =
                (1.4.2.4.1)INFERRED FROM:Table of Values Citation(LNX,34567)[TAG] = XYZ
.
.
.
    (1.10)CONTAINS:Findings(LNX,15671230)[CONTAINER] = {SEPARATE}
        (1.10.1)HAS CONCEPT MOD:Finding Site(SRT,G-C0E56)[TAG] = MOD Finding(SRT,T-F1300)
        (1.10.2)CONTAINS:Depth Index(LN,16723-7)[NUM] = 9.81 centimeter
            (1.10.2.1)HAS PROPERTIES:Normal Range Lower Limit(SRT,R-10751)[NUM] = 9.61 centimeter
            (1.10.2.2)HAS PROPERTIES:Normal Range Upper Limit(SRT,R-0055D)[NUM] = 21.5 centimeter
            (1.10.2.3)HAS PROPERTIES:Normal Range Authority(LNX,121528)[TAG] = AFI by GA, Moore et al.(99ALOKA,A18875-005)
In the above, if I am searching for [NUM] = 9.81 I would find my value on line (1.10.2). I would want to parse up that line, store all the lines that are one below that node (1.10.2.x), and store the categories above it (1.10), and (1).

almostkorean
Jul 9, 2001
eeeeeeeee
I'm stumped..here's my code:

code:
     System.out.print(type + " # Lanes: " + numLanes + " Len: " + length);
     System.out.print(" direction var: " + direction);
      if(direction == "E")
         System.out.println(" Dir: East");
      else
         System.out.println(" Dir: South");
      System.out.println("    Loc: (" + row + ", " + col + ") Col: " + colorR + " " + colorG + " " + colorB);
and my output is:

code:
Road # Lanes: 4 Len: 122 direction var: E Dir: South
    Loc: (105, 5) Col: 179 179 179
So why is it not going into the if block?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
You shouldn't compare objects with ==. Trying using the .equals method of the string.

if( direction.equals("E") )

almostkorean
Jul 9, 2001
eeeeeeeee

MEAT TREAT posted:

You shouldn't compare objects with ==. Trying using the .equals method of the string.

if( direction.equals("E") )

ohhh yeahhh. Thank you

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Lazlo posted:

I need a way to search for a value through a hierarchical list, while keeping track of each descent made. The hierarchy may have an arbitrary number of levels (nodes), though probably less than ten.

I think I can come up with a way to parse each line up using a Scanner, but I can’t think of a good way to keep track of where I’ve been in the hierarchy. Is there a package I could use to make navigating through the list easier?

Example of the input I want to process:
code:
    (1.1)HAS SDE CONTEXT:Subject Class(LNX,13254)[TAG] = Person(LNX,121025)
    (1.2)HAS SDE CONTEXT:Subject Name(LNX,134945)[PNAME] = NAME
    (1.3)CONTAINS:Person Characteristics(LNX,1435934)[CONTAINER] = {SEPARATE}
        (1.3.1)CONTAINS:SubCatagory1(LN,189316-6)[NUM] = 3 no units
        (1.3.2)CONTAINS:SubCatagory2(LN,13177-6)[NUM] = 2 no units
    (1.4)CONTAINS:Summary(LNX,1216511)[CONTAINER] = {SEPARATE}
        (1.4.1)CONTAINS:CDF(LN,15255-2)[DATE] = 20070524
        (1.4.2)CONTAINS:Con Summary(LNX,134568)[CONTAINER] = {SEPARATE}
            (1.4.2.1)HAS SDE CONTEXT:Con ID(LN,4321-1)[TEXT] = A
            (1.4.2.2)CONTAINS:Con X(LN,457621-1)[NUM] = 151 day
            (1.4.2.3)CONTAINS:Composite Age(LN134346-5)[NUM] = 133 day
            (1.4.2.4)CONTAINS:LGT rank(LN,123647-1)[NUM] =
                (1.4.2.4.1)INFERRED FROM:Table of Values Citation(LNX,34567)[TAG] = XYZ
.
.
.
    (1.10)CONTAINS:Findings(LNX,15671230)[CONTAINER] = {SEPARATE}
        (1.10.1)HAS CONCEPT MOD:Finding Site(SRT,G-C0E56)[TAG] = MOD Finding(SRT,T-F1300)
        (1.10.2)CONTAINS:Depth Index(LN,16723-7)[NUM] = 9.81 centimeter
            (1.10.2.1)HAS PROPERTIES:Normal Range Lower Limit(SRT,R-10751)[NUM] = 9.61 centimeter
            (1.10.2.2)HAS PROPERTIES:Normal Range Upper Limit(SRT,R-0055D)[NUM] = 21.5 centimeter
            (1.10.2.3)HAS PROPERTIES:Normal Range Authority(LNX,121528)[TAG] = AFI by GA, Moore et al.(99ALOKA,A18875-005)
In the above, if I am searching for “[NUM] = 9.81” I would find my value on line (1.10.2). I would want to parse up that line, store all the lines that are one below that node (1.10.2.x), and store the categories above it (1.10), and (1).

As long as this file format is a regular language and not gigantic, I'd personally write a small parser in ANTLR. You'd end up writing a baby parser anyhow, but with ANTLR you have a ton of tools to help you, plus it's drat good at its job.

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
alright guys, i got another linked list question for you. How do i delete a Node from the list? I have this code written:
code:
public void removeAddressIter(String firstNamex)
	{
		Node nrp = head;
		if(nrp == null)
		{
			System.out.println("Address Book is empty. Please add to the Address Book" +
					"before trying to delete a person.");
		}
		else
		{
			if(nrp.getFirstName() == firstNamex)
			{
				head = nrp.getRp();	//turns the head into the next node
			}
			else	//looking inside the list
			{
				Node parent, child;
				parent = head;
				child = parent.getRp();
				while(child != null)
				{
					if(child.getFirstName() == firstNamex)
					{
						parent.setRp(child.getRp() );
						return;
					}
					parent = parent.getRp();
					child = parent.getRp();
				}
			}
		}
	}
however, for some reason it doesn't delete the node. This is how my teacher told the class to delete a node, but I guess it's not working. Any help?

the onion wizard
Apr 14, 2004

Sarah Sherman posted:

however, for some reason it doesn't delete the node. This is how my teacher told the class to delete a node, but I guess it's not working. Any help?

MEAT TREAT posted:

You shouldn't compare objects with ==. Trying using the .equals method of the string.

if( direction.equals("E") )

lmao zebong
Nov 25, 2006

NBA All-Injury First Team

triplekungfu posted:


ah poo poo, i always forget about compareTo(). thanks.

crm
Oct 24, 2004

Sarah Sherman posted:

ah poo poo, i always forget about compareTo(). thanks.

equals()

Lazlo
May 1, 2002
Lately I've come to realize that I have certain materialistic needs.

TRex EaterofCars posted:

As long as this file format is a regular language and not gigantic, I'd personally write a small parser in ANTLR. You'd end up writing a baby parser anyhow, but with ANTLR you have a ton of tools to help you, plus it's drat good at its job.

Ok, ANTLR seems pretty cool, but as a novice, how difficult would it probably be to learn to use this tool, and then get it to work with the java code I've already got?

So far, working from their tutorials and faq, I still can't get antlr to actually run yet.

The Light Eternal
Jun 12, 2006

A man who dares to waste one hour of time has not discovered the value of life.
Hello. I'm writing a program in java to random seed a tournament.

My initial inclination was to do this:
Scanner input = new Scanner(System.in);
System.out.println("Enter number of players:");
Integer num = input.nextInt();
Integer count=0;
while (count <= num)
{
Random r = new Random();
int randint = r.nextInt(num);
System.out.println(randint);
count++;
}
But it creates repeats. I learned from #cobol that I need to add the numbers to a list and then shuffle the list. How do I do that?

oh no computer
May 27, 2003

Collections.shuffle() shuffles a List object. You'll probably want an Arraylist.

The Light Eternal
Jun 12, 2006

A man who dares to waste one hour of time has not discovered the value of life.

BELL END posted:

Collections.shuffle() shuffles a List object. You'll probably want an Arraylist.

Okay, but how exactly do I do that?

oh no computer
May 27, 2003

Not at a compiler so I can't check this, but something along the lines of:

code:
ArrayList<Integer> array = new ArrayList<Integer>(num);
for (int i = 0 ; i < num ; i++)
{
    array.add(i)
}
Collections.shuffle(array);

csammis
Aug 26, 2003

Mental Institution

The Light Eternal posted:

But it creates repeats. I learned from #cobol that I need to add the numbers to a list and then shuffle the list. How do I do that?

I must not've been in #cobol at the time :v:

Shuffling isn't the answer here. You're creating a new Random on each iteration of the while loop. By default, the Random object is seeded by the system time, which in a fast loop will be the same on each iteration of the loop. Two Random objects initialized with the same seed are guaranteed to produce the same pseudorandom sequence. That's why you're getting repeats.

Always seed random objects outside of a loop unless you know why you're doing it any other way.

code:
Random r = new Random();
while (count <= num)
{
  int randint = r.nextInt(num);
  System.out.println(randint);
  count++;
}

oh no computer
May 27, 2003

While that's true, it won't necessarily stop the same number coming up more than once.

csammis
Aug 26, 2003

Mental Institution

BELL END posted:

While that's true, it won't necessarily stop the same number coming up more than once.

True, though it's a hell of a lot less likely. Anyway, I suspect his "repeats" problem looks something like this:

4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3

Besides, how is shuffling an answer to "preventing repeats" in the first place? It might space them further apart from each other, but they'd still be repeated in the sequence. Seriously shuffling is just not a solution to anything that has to do with repeats, unless your requirement is that two identical numbers can't be next to each other - and even then it's not a guarantee.

oh no computer
May 27, 2003

In my example (assuming it works) you're creating an array of [1, 2, 3, 4, 5...] and then shuffling it so you know the numbers are different.

I think that's what he was asking, maybe I'm reading the problem wrong.

Adbot
ADBOT LOVES YOU

csammis
Aug 26, 2003

Mental Institution

BELL END posted:

In my example (assuming it works) you're creating an array of [1, 2, 3, 4, 5...] and then shuffling it so you know the numbers are different.

I think that's what he was asking, maybe I'm reading the problem wrong.

The fact remains that shuffling is never ever ever going to eliminate repeats or reliably spread them around if your sequence has repeats in it in the first place.

I don't have a javac in front of me, but here's The Light Eternal's code in C#:

code:
Console.WriteLine("Enter number of players:");
int num = Int32.Parse(Console.ReadLine().Trim());
int count = 0;
while(count <= num)
{
    Random r = new Random();
    int randint = r.Next(num);
    Console.WriteLine(randint);
    count++;
}
This produces output like:
code:
Enter number of players:
4
1
1
1
1
1
code:
Enter number of players:
7
1
1
1
1
1
1
1
1
code:
Enter number of players:
2
0
0
0
Now if I seed it outside the loop:

code:
Enter number of players:
4
2
0
1
2
1
There, random numbers all below the value entered. There are still repeats in the sequence; shuffling won't solve that "problem," but I don't think that was the issue that TLE was having.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply