Note that there are some explanatory texts on larger screens.

plurals
  1. PORandomWalk - New task
    primarykey
    data
    text
    <p>I've come again to ask for your help. I have this piece of code:</p> <pre><code>import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.util.HashMap; public class RandomWalk { public static final int NBR_WALKS = 1000; public static final int NBR_STEPS = 500; public static void main(String[] args) { /*PrintStream out; try { out = new PrintStream(new FileOutputStream("/Volumes/DATA/Desktop/RandomWalkResultater.txt", true)); System.setOut(out); } catch (FileNotFoundException e) { e.printStackTrace(); }*/ HashMap&lt;Integer, Integer&gt; totals = new HashMap&lt;&gt;(); for (int i = -NBR_STEPS; i &lt;= NBR_STEPS; i++) { totals.put(i, 0); } System.out.println(""); for (int i = 0; i &lt; NBR_WALKS; i++) { int total_value = 0; for (int j = 0; j &lt; NBR_STEPS; j++) { int L = (int) (Math.random() * 2); total_value += (L == 0) ? -1 : 1; } System.out.println("For randomwalk nummer " + i + " var slut punktet " + total_value); totals.put(total_value, totals.get(total_value) + 1); } System.out.println(" "); for (int i = -500; i &lt; 500; i++) { System.out.println("Du landede på slut punktet "+i+" følgende gange: "+totals.get(i)); } System.out.println(" "); System.out.println("Til excel arket:"); System.out.println("x-værdierne:"); for (int i = -500; i &lt; 500; i++) { System.out.println(i); } System.out.println(""); System.out.println("y-værdierne:"); for (int i = -500; i &lt; 500; i++) { System.out.println(totals.get(i)); } } } </code></pre> <p>And I need to add a feature. I want this program to output the following (I've tried and I don't think I completely understand it because it's not doing as I want):</p> <p>The average value. It'll do 1000 walks with 500 steps in them, so theoretically it could end up in -500 or +500. So to calculate the average value I need to say (let's call it a) a = ((Where it landed * how many times it landed on that spot)+(Where it landed+1 * how many times it landed on that spot+1)+..)/number of walks, in this case 1000)</p> <p>Example: a = ((-500*0)+(-499*0)...(10*3)...(500*0))/(1000)</p> <p>Can you please help me with this? I've tried for over a day and I can't seem to wrap it up :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload