Noob here. I've tried using the other answers for random numbers, but they don't seem to accomplish what I'm trying to do.  So... I'm trying to make a method that catches a random fish in a pond. It will catch multiple fish in a row that are stored in an array, and I basically want it to avoid catching null, and still catch a fish every time it is run. Here's the method.  public String catchAFish(){    int x = (int)(numFish * Math.random());   pond[x] = pond[numFish+1];   pond[numFish+1] = null;   numFish--;   return ("You have caught "+pond[x]); }   The last fish in the array moves into the spot of the fish that was caught. I tried a function in a for loop before, and it accomplished the same thing. The problem is that the number of possible fish to catch changes whenever a fish is caught, so I can't just set a range of "0-10".
4701
How do I generate a random int value in a specific range? I have tried the following, but those do not work: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum);  Bug: randomNum can be bigger than maximum. Attempt 2: Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum =  minimum + i;  Bug: randomNum can be smaller than minimum.
113807
Why does the following code not work?  I want to instantiate prefabs in a list and then use  it as a GameObject like that :  public GameObject prefab;  void Start() {   ArrayList players = new ArrayList();    for(int i=0; i&lt;5; i++)   {         players.Add( (GameObject)Instantiate(prefab));   }    players[2].transform.position = new Vector3(1, 1, 1);  }   there is an error message
119103
Omg this is driving me crazy seriously, it's a subproblem for a bigger problem, and i'm stuck on it.  Anyways i need the number of ways to pick $x[1]$ ammount of objects type $1$, $x[2]$ ammount of objects type $2$, $x[3]$ ammounts of objects type $3$ etc etc such that $$x[1] + x[2] + \ldots x[n] = k\;.$$ Order of the objects of course doesn't matter.  I know there is a simple formula for this something like $\binom{k + n}n$ or something like that i just can't find it anywhere on google.
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
120455
I have been given $25$ objects numbered from $1$ to $25$ and a set $S = \{-2, -1, 0, 1, 2\}$. I have to choose a random number from the given set and add it to all the objects. I have to do this $5$ times. What will be the expected sum of all objects after all $5$ turns?
52121
I need to generate a random integer between 1 and n (where n is a positive whole number) to use for a unit test. I don't need something overly complicated to ensure true randomness - just an old-fashioned random number.  How would I do that?
109203
I was reading the Math.random() javadoc and saw that random is only psuedorandom.    Is there a library (specifically java) that generates random numbers according to random variables like environmental temperature, CPU temperature/voltage, or anything like that?
98914
The MSDN documentation on  describes 3 contradicting rules for how the method should work.     If two objects of the same type represent the same value, the hash function must return the same constant value for either object. For the best performance, a hash function must generate a random distribution for all input. The hash function must return exactly the same value regardless of any changes that are made to the object.   Rules 1 &amp; 3 are contradictory to me.  Does Object.GetHashCode() return a unique number based on the value of an object, or the reference to the object. If I override the method I can choose what to use, but I'd like to know what is used internally if anyone knows.
42562
How could I go about detecting (returning true/false) whether an ArrayList contains more than one of the same element in Java?  Many thanks, Terry  Edit Forgot to mention that I am not looking to compare "Blocks" with each other but their integer values. Each "block" has an int and this is what makes them different. I find the int of a particular Block by calling a method named "getNum" (e.g. table1[0][2].getNum();
62327
On googling I got     Random numbers are numbers that occur in a sequence such that two conditions are met: (1) the values are uniformly distributed over a defined interval or set, and (2) it is impossible to predict future values based on past or present ones. Random numbers are important in statistical analysis and probability theory.   What if I want to pick a random integer from the number line ? The number line consists of infinitely many integers. My favorable event is to get a number (random) and the number of events possible is the complete number line i.e. infinity. I won't be considering any limits here as our number line doesn't tend to infinity but does contain infinitely many elements.  Now the probability of getting a random number  P(E) = 1 / ∞ = 0   So I won't be able to get a random number, ever. This implies, never would I ever be able to get a random number.  That's absurd as many of our science topics do depends on things being random.
31694
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.   What is a simple solution? What is an effective solution to reduce memory usage or run on a slower CPU?
104870
I've been working on this task (see screenshot).     Here's my answer to it so far:  var sum_pairs=function(ints, s){   //your code here    var ptr_1 = 0,       ptr_2 = 0,       i = 0,       j = 0,       len = ints.length,       j_min = len,       arr_sum = [];    for (i; i &lt; len-1; i++){     for (j = i+1; j &lt; len; j++){       if(ints[i] + ints[j] === s){         if (j &lt; j_min){           j_min = j;           arr_sum = [parseInt(ints[i]), parseInt(ints[j])]         }       }     }   }  return ( (arr_sum.length === 0)? undefined : arr_sum );   }   However, while the tests all pass, it throws an STDERR about 'optimising the code':    How might I be able to optimise my code further?
29728
In other words, can I do something like  for() {     for {        for {        }     } }   Except N times? In other words, when the method creating the loops is called, it is given some parameter N, and the method would then create N of these loops nested one in another?  Of course, the idea is that there should be an "easy" or "the usual" way of doing it. I already have an idea for a very complicated one.
80428
I have a class like this:  public class myClass {   public List&lt;myOtherClass&gt; anewlist = new List&lt;myOtherClass&gt;;    public void addToList(myOtherClass tmp)   {     anewList.Add(tmp);   }  }   So I call "addToList" a hundred times, each adding a unique item to the list.  I've tested my items to show that before I run the "addToList" method, they are unique.  I even put a line in to test "tmp" to make sure it was what I was expecting.  However, when I do this (lets say myClass object is called tmpClass):  int i = tmpClass.anewList.Count(); for (int j = 0; j&lt;i; j++) {    //write out each member of the list based on index j... }   I get the same exact item, and it's the last one that was written into my list.  It's as if when I add, I'm overwriting the entire list with the last item I've added.    Help?  This makes no sense. I've also tried List.Insert, where I'm always inserting at the end or at index 0.  Still no dice.  Yes, I'm doubly source my indexing is correct and when I do my test I'm indexing through each of the elements.  :)  UPDATE: Okay, I tried this and still had the same problem:  foreach(myOtherClass tmpC in tmpClass.anewList) {         Console.WriteLine(tmpC.theStringInMyClass.ToString()); }   and still for each of the 100 items, I got the same string output... I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.  -Adeena    Okay, I tried this and still had the same problem:  foreach(myOtherClass tmpC in tmpClass.anewList) {     Console.WriteLine(tmpC.theStringInMyClass.ToString()); }   and still for each of the 100 items, I got the same string output...  I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.  -Adeena
650
What is the best way to randomize the order of a generic list in C#? I've got a finite set of 75 numbers in a list I would like to assign a random order to, in order to draw them for a lottery type application.
9770
I'm sure you all know the behaviour I mean - code such as:  Thread thread = new Thread(); int activeCount = thread.activeCount();   provokes a compiler warning.  Why isn't it an error?  EDIT:  To be clear: question has nothing to do with Threads.  I realise Thread examples are often given when discussing this because of the potential to really mess things up with them.  But really the problem is that such usage is always nonsense and you can't (competently) write such a call and mean it.  Any example of this type of method call would be barmy.  Here's another:  String hello = "hello"; String number123AsString = hello.valueOf(123);   Which makes it look as if each String instance comes with a "String valueOf(int i)" method.
33662
I've got a random number generator that yields values between 0 and 1, and I'd like to use it to select a random point on the surface of a sphere where all points on the sphere are equally likely.  Selecting the longitude is easy as all lines of longitude are of equal length. x × 360&deg;.  Latitude, on the other hand, requires that 0&deg; (the equator) is twice as likely to be selected than 60&deg;. 90&deg; would have an almost zero chance of being selected.