• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Question about probabilities

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I a almost a total beginner in Java and right now coding my first a little bit serious project. I have made a few arrays with strings and a few lines of real code. So far all goes well.

Now i came to a problem of creating a code which should according to mathematical probability generate one of 4 different labels, lets call them bad, normal, good, excellent, and chances for generating each of them is different.

Lets say bad 10%, normal 60%, good 20%, excellent 10%.

This looks simple to me, but as i am pretty new in programming i could be wrong.
If anyone can tell me how to solve this little problem?
Thanks.
Hope my post is ok, as i am new here. Thank you.
 
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you come across any classes that will produce ”random“ numbers? Or other similar methods? If not, look in the API documentation, and use the Search box at the top right.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, i worked with extracting random strings from arrays, in this project i am working on. I will look at the documentation and do some search
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain more. I can think of several ways you can achieve what you want. Don't expect a quick reply from me; I am busy now.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, i can wait. I am happy someone is willing to help me with this.

So, i made three arrays like this;

String[]imena;
   imena = new String[48];
   imena[0] = "Goran";
....etc..

And then i extracted random Strings from all three arrays with a code like this;

int random = (int) (Math.random()*48);
   int prezime = (int) (Math.random()*76);
   int poza = (int) (Math.random()*11);

And printed what i need with this code;

System.out.println( "Ime igrača je: " + imena[random]);
   System.out.println( "Prezime igrača je: " + prezimena[prezime]);
   System.out.println( "Pozicija: " + pozicija[poza]);

Now i would like to make a program to make qualitative differences and print that. Later i have other problems with the values I get, but that is another question.
 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When posting code please UseCodeTags (<- link).

You are using an obsolete form of generating a random integer. The preferred way is to first create a Random object and then use that.
It looks like you are displaying 3 random strings. Where does the probability come in to play? Are some of the strings in the arrays duplicates?
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
Yes, i am using three array to create random strings. Well, i would like to explain what i am trying to do. I would like to create a little application that would display attributes of randomly created football player. I do that as an exercise and a way to learn this program and make it fun for me at the same time.

So, basically i would like the application to display details of a player which will be randomly generated but with certain probabilities in certain instances. To be more precise. I have an idea that player will have 3 main groups of characteristics. Each group will have a certain probability of having the starting value level. So basically that is what i would like to do
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So lets say staring value in one group of characteristics  of a player is bad.. that means that the player in that group would generate values for each skill in that group from lets say 1 to 7 randomly, if a player has lets say excellent as a starting value he would randomly generate values of lets say 5 to 11. And i would like to create three such groups, that is an idea.
 
Master Rancher
Posts: 4062
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like what you're asking about now is this part:

Dario Castelli wrote:Lets say bad 10%, normal 60%, good 20%, excellent 10%.



Based on what you've show you can do in the other code...  Let's say you generate a random number in the range 0-9.  Is there a way you can convert this to a string with a value of either "bad", "normal", "good", or "excellent"?  Where "bad" occurs 1/10 of the time, and "normal" occurs 6/10 of the time, etc?  Could you write a method that could take that integer and make a String out of it?
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent question. That is much more complex part of the project i would like to do. The next step in the program. And, no i don't know how to do that.. For now i only wanted to create these starting groups, and learn a bit of how probabilities look like in java code.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:It sounds like what you're asking about now is this part:

Dario Castelli wrote:Lets say bad 10%, normal 60%, good 20%, excellent 10%.



Based on what you've show you can do in the other code...  Let's say you generate a random number in the range 0-9.  Is there a way you can convert this to a string with a value of either "bad", "normal", "good", or "excellent"?  Where "bad" occurs 1/10 of the time, and "normal" occurs 6/10 of the time, etc?  Could you write a method that could take that integer and make a String out of it?



Hmm..  so now i would like to see how the code for those probabilities would look like. To explain, the way i visioned it the player has three main groups of characteristics. Technical, Mental and Physical, further on Technical is divided in 10 skills, mental 10 skills and physical 6 skills (attributes). So the player should be generated like this (i envisioned 8 starting levels of skills for each of three main groups, this simple values of "good, bad etc.. are only a simplification) So, lets say that a player gets by the way of probability starting value of lets name it "average" in technical group, that means he would generate random values from certain number to certain number in that group of skills.. and the process would be repeated for two other groups. So, in short.. first a group of skills gets the starting value according to which it generates values in a certain range of number values in that particular group, then it generates it for the next two groups in the same way.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have got a lot more accomplished than I thought at first.

Dario Castelli wrote:. . .

One space after [] please. Try an array initialiser. It is much quicker to write and more reliable; there is no risk of leaving uninitialised elements or overrunning the size of the array:-Make sure to break the line so it doesn't become too long.

And then i extracted random Strings from all three arrays with a code like this;

int random = (int) (Math.random()*48); . . .

As you will see here←(read the whole thre‍ad), I don't like arithmetic with Math#random(). It is more reliable to use myRandom.nextInt(...).
Don't hard‑code 48 or similar numbers. Use myRandom.nextInt(myArray.length). Read the documentation for Random#nextInt(int), and you will find that the results exactly fit the size of the array.

. . .. . .

Yes, that line is more complicated and denser to read. So some people would think that is poor style. I would suggest that you are writing the name of the array twice in the same line, so you are getting better reliability (same name twice), and that makes the complicated line better. You have some repeated code there. Maybe you can use printf rather than println and get all your printing into one statement:-You can replace those lines by a method call as Mike Simmonds suggested. Maybe a static method:-edit: You will find a very similar method here.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for this post, very helpful and informative, now I have to study this!
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I tried to change the code as suggested, but something went wrong :S



I got the following error;
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: random.nextInt
at javaapplication4.JavaApplication4.main(JavaApplication4.java:23)
C:\Users\Darijo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 6 seconds)
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't created an object of type Random named random.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You haven't created an object of type Random named random.



Well, as i already said i am almost a total beginner. Could you be so kind to tell me the solution how can i make this code to work. What should i write and where? Thanks.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Hint: You need a new Random.





Something like this? It actually works, but still reporting errors.

RESULT:

Name: DinkoTOPIĆurname: CM

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
at java.util.Formatter.format(Formatter.java:2519)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:28)
Position: C:\Users\Darijo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is here:
An percent sign (%) starts a new marker that will be filled in with the data you supply.  Everything else is a literal.  I suspect you don't want the percent sign in front of Surname.  Or maybe you want another marker there and you left out the marker letter.
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get rid of the second % in each specifier., ...
for example,

For selecting based on probability, generate a random integer between 1 and 100 inclusive.
Then if the value is in the range 1 to 10 (10 %), select "Bad".
If it is between 11 and 70 (60 %), pick "normal".
If it is between 71 and 90 (20 %), pick "good".
Otherwise (i.e., from 91 to 100) (10 %), choose "Excellent".
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Get rid of the second % in each specifier., ...
for example,


I wouldn't change (%n) to (\n).  The latter is not portable to all OSs.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Get rid of the second % in each specifier., ...
for example,

For selecting based on probability, generate a random integer between 1 and 100 inclusive.
Then if the value is in the range 1 to 10 (10 %), select "Bad".
If it is between 11 and 70 (60 %), pick "normal".
If it is between 71 and 90 (20 %), pick "good".
Otherwise (i.e., from 91 to 100) (10 %), choose "Excellent".



Thanks Fred!
Very helpful posts. Well i had a completely different code yesterday and so it distracted me from my real question but i am very glad i learned something new. Great posts!
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your Bad... ratings, you could use a series of else-ifs which would work nicely. You can also take advantage that the percentages between groups are multiples of 10 and use that as an index into an array of rating Strings.

 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now code runs fine. No errors but i still have to figure out how to print this properly.

Result;

run:
Name: Josip/nSurname: Topić/nPosition: RB/nBUILD SUCCESSFUL (total time: 0 seconds)
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your format string, don't use /n or \n, use %n.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:In your format string, don't use /n or \n, use %n.




Done. It prints properly
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, if you are only using %s as the format (and not something more complicated, for example for real  number formatting) I would just use:

It is easier to read, and easier to modify if needed.
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Get rid of the second % in each specifier., ...
for example,

For selecting based on probability, generate a random integer between 1 and 100 inclusive.
Then if the value is in the range 1 to 10 (10 %), select "Bad".
If it is between 11 and 70 (60 %), pick "normal".
If it is between 71 and 90 (20 %), pick "good".
Otherwise (i.e., from 91 to 100) (10 %), choose "Excellent".



I made the code, and It looks like this:




I decided to generate a lager range, because i wanted the highest values to be very rare.
Program works fine, only there is probably a simpler way ( or at least more elegant) to do this?
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . An percent sign (%) starts a new marker . . . left out the marker letter.

He has forgotten the n. That leaves %S which is a valid tag, but it puts all the other arguments out of phase. For percent, use the %% tag.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dario Castelli wrote:. . . I made the code . . .

Why are you using <= throughout? Use < because it is easier to understand. Also put one space to the right of the <. The problem is that you are thinking in terms of ℕ₁ numbers rather than plain simple ℕ. Start from 0; you don't need the + 1.


. . . there is probably a simpler way ( or at least more elegant) to do this?

And when I altered your code, I noticed that you had introduced a logic error because you wrote < 1700 in line 3, so that is a 16.99% chance rather than a 17% chance. The final line doesn't need else if; it should be a plain simple if.
I would simplify that further; this is one instance where I would use Math#random() because I am not doing any arithmetic with it:-You would of course not get any credit for correct indentation. Cramming the code so close together is a sure‑fire recipe for making it illegible. Use spaces rather than tabs.
You still have a lot of repeated code. Let's see how much of that repetition we can remove.
 
Bartender
Posts: 4691
183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In cases like this I like to work with TreeMaps. For instance:
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An hour ago. I wrote:. . . The final line doesn't need else if; it should be a plain simple if. . . .

And did anybody notice my mistake? I should have written

. . . The final line doesn't need else if; it should be a plain simple else. . . .

 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . I like to work with TreeMaps. . . .

Now, that's a nice suggestion Please explain more about the floor and ceiling methods.
 
Piet Souris
Bartender
Posts: 4691
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

An hour ago. I wrote:. . . The final line doesn't need else if; it should be a plain simple if. . . .

And did anybody notice my mistake? I should have written

. . . The final line doesn't need else if; it should be a plain simple else. . . .


Missed it  

You also don't need the curly braces. Makes it a lttle more readable.  If there is only one expression following an if I always use this style:

Saves a few lines and is very readable, certainly compared to:
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the random integer according to this code:



Now i would like to get a value of a  new integer "techa" which will be in a range + or minus 2 from this randomly generated integer using this dubious integer as a starting point of reference.

 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
techa = dubious + randomGenerator.nextInt(5) - 2;
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can't figure it out in your head, pencil and paper helps. ;)
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do, or at least i am on a good path to figure it out.
Now i need to connect this with a previous code, under certain conditions to get what i want.
Trying..
 
Dario Castelli
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhhh..

i have this line of code:



it generates a random integer "tech_poor" in a range from 3 to 6, as i wanted it.

and now with Carey's help the next line of code generates a random integer "techa" which uses "tech poor" integer
in a way that the value of "techa" is + or - 2 of integer tech_poor.



But now another problem arises. I need a program to repeat this operation three times, not just once.
So it gives three values.
How to do this? I need techa to have "three heads" so to speak.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic