Creating a program where a random number is generated using math.random which i have put into a function and called this function in the program to make things tidier but the random number that gets outputted is always what the integer secret_number is set to, and i am not sure how to fix this to create a random number. I would like to keep the math.random in the function and use parameters  package guessinggame3; import java.util.Scanner; //imports scanner to read from keyboard import java.util.ArrayList;   public class GuessingGame3 { //start of public class  static Scanner kboard = new Scanner(System.in); //calls scanner    public static void main(String args[]) //start of main  {  System.out.println("Welcome to the guessing game, the computer will  generate a random number that you have to guess, good luck!"); //opening  message explaining how to play   int secret_number = 0; int number_of_guesses = 0; int user_guess ; ArrayList&lt;Integer&gt; entered_numbers = new ArrayList&lt;Integer&gt;();   generate_random_number(secret_number); //calls he function  generate_random_number to say the random number has been generated      for(int i=0; i&lt;20;i++) { // start of for loop sets user attempts to 20   System.out.println("Please make your guess"); //asks user to enter make  their guess              user_guess = kboard.nextInt(); number_of_guesses++; // adds on to the counter after each user guess   if(entered_numbers.contains(user_guess)) //checks if entered number =  number stored in array { System.out.println("You have already entered this number"); //displays  error message if user enters same number again continue; }  else  { entered_numbers.add(user_guess); if (user_guess == secret_number) System.out.println("Your guess is corret you win!"); }  if (user_guess &lt; secret_number) { System.out.println("Guess is too low"); }  if (user_guess &gt; secret_number)  { System.out.println("Your guess is too high"); }   System.out.println (20 - number_of_guesses + " Guesses remaining");  //Tells the user how many guesses they have remaining    } //end of for loop  } //end of main  static void generate_random_number(int secret_number)  //start of function  generate_random_number function passing secret_number as a parameter {        secret_number = (int)(Math.random()*100) + 1; //generates random number  between 0 and 100 System.out.println("The computer has generated it's number"); //lets the  user know that the random number has been generated } //end of function generate_random_number   } //end of public class
121
I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation?
59865
I'm trying to make a game where the damage you deal is rolled by a dice. It all works. However, it keeps on giving the same values in a row so it isn't entirely random  ini_open("Health.ini"); attack = ini_read_real( "Attacks", "Felix", 0); ini_close(); global.damage=0 repeat(attack){ global.damage=irandom(6) }   if (global.damage ==0){instance_create_depth(300, 400, 0, obj_numbers0)} if (global.damage ==1){instance_create_depth(400, 400, 0, obj_numbers1)} if (global.damage ==2){instance_create_depth(500, 400, 0, obj_numbers2)} if (global.damage ==3){instance_create_depth(600, 400, 0, obj_numbers3)} if (global.damage ==4){instance_create_depth(700, 400, 0, obj_numbers4)} if (global.damage ==5){instance_create_depth(800, 400, 0, obj_numbers5)} if (global.damage ==6){instance_create_depth(900, 400, 0, obj_numbers6)}   The attack value is 1 because it is only one dice that is used for damage for now. The software I am using is Game Maker Studio 2
97272
What is the most efficient way to get a random output with my commands? In Java there are tons of ways to do this, but a lot of them don’t seem to work for Bedrock Edition.
39841
How to use the  method?  Say I want to find number of combinations I can get with $x_1+x_2+x_3+x_4=22$,  where $x_i\in\mathbb{N}$.  Is this the correct time to apply the method?  This is being repurposed in an effort to cut down on duplicates, see here:     and here:
27140
This is a problem I've ran into several times, and I imagine that others have ran across it too.    Basically, I have a complicated equation, and I want the user to type in the values needed to calculate the results.  (For what it's worth, there are good default values for the variables in the equation.)  I've constructed a GUI that shows the user the names of the variables/fields that need to be provided, along with a control (like a numeric up/down, slider, etc.) to enter the values of the field.  Below that, I have an area that shows the output of the equation.  So my question is this.  Should my results area automatically change the calculated results whenever the user makes any changes to any of the input fields, or should they push a "Calculate" button to generate the results?  One other thing that probably applies is that the user is not trying to "get the right answer" out of the results.  They shouldn't be using the output to then go back and modify the input to get a better answer.  Are there any benefits to requiring the button press?  Is the extra click to see the results worth those benefits?
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
50029
How do I generate a random number between 0 and n?
13424
I want to make a mini game in Minecraft were I need a random command generator for 9 commands. I need a random command to give the player a random item. I can't do the command block in the dispenser any more because it will just drop out instead of being placed. What different ways are there for generating randomness in commands, and which one would be best for my use? This question is similar to the one about , but I am specifically attempting to get randomness when using commands - the redstone solutions do not apply here.
30011
I have the following probability function:  $$\text{Prob} = \frac{1}{1 + e^{-z}}$$  where  $$z = B_0 + B_1X_1 + \dots + B_nX_n.$$  My model looks like  $$\Pr(Y=1) = \frac{1}{1 + \exp\left(-[-3.92 + 0.014\times(\text{gender})]\right)}$$  I understand what the intercept (3.92) means, but I'm now sure how to interpret 0.014. Are these still log odds, odd ratios, or can I now assert that for each incremental odds change is gender, females are 0.014 more likely to win than men. Basically, how am I to interpret the 0.014?   Basically, I want to take the probability function and actually implement it in Java for a specific program that I'm writing, but I'm just not sure if I'm understanding the function correctly to implement it in Java.  Java code example:   double p = 1d / (1d + Math.pow(2.718d, -1d * (-3.92d + 0.014d * bid)));
23909
Looking for reviewers to suggest optimization and improvements, and to evaluate coding practices.  final class Variables {     private final int x;     private final int y;      Variables(int x2, int y2) {         this.x = x2;         this.y = y2;     }      public int getX() {         return x;     }      public int getY() {         return y;     }      @Override public String toString()  {          return "x = " + x + " y = " + y;     } }  public final class FindMissing {      private FindMissing() {};      /**  * Takes an input a sorted array with two variables that repeat in the  * range. Remaining variable are consecutive. and returns two variables that repeats  *   * eg: for a range of 1,2,3,4, a valid input is [1, 4], [1, 3] etc.  *   * @param a : the sorted array.  */ /*  * Why is start needed ?  * If start is not given then if a1 is empty how do we know if missing numbers were  * [1, 2] or [4, 5] ??.   */ public static Variables sortedConsecutiveTwoMissing (int[] a, int startOfRange) {     if (a == null) throw new NullPointerException("a1 cannot be null. ");      int low = startOfRange;     // calculating the highest value of the range. it needs to be 2 more than length since all elememts are consecutive.     int high = startOfRange + (a.length - 1) + 2;      int i = 0;     boolean foundX = false;     int x = 0;       while (i &lt; a.length) {         if (a[i] != low) {             if (foundX) {                 return new Variables(x, low);             } else {                 x = low;                 foundX = true;             }         } else {             i++;         }         low++;     }      return new Variables(x, high);  }  public static void main(String[] args) {         // unsortedConsecutiveTwoMissing         int[] a6 = {1, 4};         System.out.println("Expected 2 and 3, Actual " + sortedConsecutiveTwoMissing(a6, 1));         // unsortedConsecutiveTwoMissing         int[] a7 = {-4, -1};         System.out.println("Expected -3 and -2, Actual " + sortedConsecutiveTwoMissing(a7, -4));         int[] a8 = {-4, -3, -2, -1};         System.out.println("Expected 0 and 1, Actual " + sortedConsecutiveTwoMissing(a8, -4));         int[] a9 = {5};         System.out.println("Expected 4 and 6, Actual " + sortedConsecutiveTwoMissing(a9, 4));         }      }
7553
I'd like to generate unique random numbers between 0 and 1000 that never repeat (i.e. 6 doesn't show up twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is this possible?
2899
I need a 'good' way to initialize the pseudo-random number generator in C++. I've found  that states:     In order to generate random-like   numbers, srand is usually initialized   to some distinctive value, like those   related with the execution time. For   example, the value returned by the   function time (declared in header   ctime) is different each second, which   is distinctive enough for most   randoming needs.   Unixtime isn't distinctive enough for my application. What's a better way to initialize this? Bonus points if it's portable, but the code will primarily be running on Linux hosts.  I was thinking of doing some pid/unixtime math to get an int, or possibly reading data from /dev/urandom.  Thanks!  EDIT  Yes, I am actually starting my application multiple times a second and I've run into collisions.
94938
In R, if I set.seed(), and then use the sample function to randomize a list, can I guarantee I won't generate the same permutation?  ie...  set.seed(25) limit &lt;- 3 myindex &lt;- seq(0,limit) for (x in seq(1,factorial(limit))) {     permutations &lt;- sample(myindex)     print(permutations) }   This produces  [1] 1 2 0 3 [1] 0 2 1 3 [1] 0 3 2 1 [1] 3 1 2 0 [1] 2 3 0 1 [1] 0 1 3 2   will all permutations printed be unique permutations?  Or is there some chance, based on the way this is implemented, that I could get some repeats?  I want to be able to do this without repeats, guaranteed.  How would I do that?  (I also want to avoid having to use a function like permn(), which has a very mechanistic method for generating all permutations---it doesn't look random.)  Also, sidenote---it looks like this problem is O((n!)!), if I'm not mistaken.
57093
I think there's a lot for me to learn about data types. Why this happens  double result = ((3/8)*100).ToString();   it gives zero .. should be 37,5 ... :(
77701
Just for fun, I am trying to find a good method to generate a random number between 1 and 10 (uniformly) with an unbiased six-sided die.  I found a way, but it may requires a lot of steps before getting the number, so I was wondering if there are more efficient methods.  My method:   Throw the die and call the result $n$. If $1\leq n\leq 3$ your number will be between $1$ and $5$ and if $4\leq n\leq 6$ your number will be between $6$ and $10$. Hence, we reduced to the problem of generating a random number between $1$ and $5$. Now, to get a number between $1$ and $5$, throw the die five times. If the $i$th throw got the largest result, take your number to be $i$. If there is no largest result, start again until there is.   The problem is that although the probability that there will eventually be a largest result is $1$, it might take a while before getting it.  Is there a more efficient way that requires only some fixed number of steps? Edit: Or if not possible, a method with a smaller expected number of rolls?
52342
I need to randomly 'sort' a list of integers (0-1999) in the most efficient way possible.  Any ideas?  Currently, I am doing something like this:  bool[] bIndexSet = new bool[iItemCount];  for (int iCurIndex = 0; iCurIndex &lt; iItemCount; iCurIndex++) {     int iSwapIndex = random.Next(iItemCount);     if (!bIndexSet[iSwapIndex] &amp;&amp; iSwapIndex != iCurIndex)     {         int iTemp = values[iSwapIndex];         values[iSwapIndex] = values[iCurIndex];         values[iCurIndex] = values[iSwapIndex];         bIndexSet[iCurIndex] = true;         bIndexSet[iSwapIndex] = true;     } }
76223
\documentclass{minimal} \usepackage{tikz} \begin{document} \pgfmathparse{random(-5,5)} \pgfmathresult \end{document}   Works, but I have a .0.0 that not supposed to be here.