Help with a beginning project

Started by
7 comments, last by RulerOfNothing 3 years, 10 months ago

Hello. I have just a little knowledge about programming, and I would like to make my simple first project in Java using Netbeans.

I would like a program to first randomly pick one out of lets say 20 names and then to generate numbers from 1-7 in ten cells as lets say attributes of that name. Also i would like to add probability to the program in a way that there is a 50% chance that the program will generate numbers randomly from 1-7 and 50% chance that it will generate numbers from lets say 2-8.

I am new here so i hope this is not something unusual. Any help with a little source code or advice on what to study in relation to this little project would be much appreciated. Thank you.

Advertisement

You may use the code tag to show your code. Java boilerplate for command line and then some hows that called in Java:

system.io.println("I picket this name: " & name );;;;;

You can always dice twice, like

var number=random()>0.5 ? random()*6+1 : random()*7+2;;;;;;;;;;;

or so. Of course you can also dice once and use cumulation.

@arnero Thank you, very helpful. I get the second line of the code but i am struggling with the first line. Let's say i have 20 names in a file and i want a program to pick randomly only 5 of them, what would that code look like?

Start simpler, make a program with an array or a list with 20 names, pick one of them, and print it.

Loading text from a file into an array or list is a separate problem that you can code somewhen in the future. Reading and writing files is more difficult than having the names in the program already.

@Alberth Thank you. Well, then i will leave loading texts for later. Now, i do know how to do arrays but i don't know how would i instruct the program to pick randomly only a few (lets say 5) names from that array. Any help?

Oh, okay. I would define a class (or stuct in C#), which has as properties (member variables + getter setter) a string and a boolean. Start with all booleans set to false. Mark every word taken.

Maybe there is some dictionary magic available, but I try to avoid frameworks and their complicated data structures.

Edit: I just remembered that Povray shuffled arrays. So you go through the array and swap every entry with any random other entry.

@Darac you type “java random” in a search engine, and get this: https://docs.oracle.com/javase/7/docs/api/java/util/Random.html

It may be a hard to read (but oracle owns Java, so this is the one and definite manual for everything Java), try it a few times, at some point you start to see the logic in it, and then it's very compact and helpful.

in more words than Oracle spends on it, you make a Random instances (Random rnd = new Random() ) and then you can use “int value = rnd.nextint(exclusive_max);” to get a random value.

In this particular case, to randomly select 5 elements from your list of names you could use:

Random rng = new Random();
for(int i=0; i<5; i++) {
	//generates a random integer in [i,names.length)
	int swapTarget = i + rng.nextInt(names.length - i);
	String temp = names[i];
	names[i] = names[swapTarget];
	names[swapTarget] = temp;
}

After running this code the first 5 elements of names will contain the random selection of names. If you require the list of names to remain in order you can either copy the list of names or use indexes instead.

This topic is closed to new replies.

Advertisement