I have two arrays called numbers and sounds. My code is below. When I use random generator, I don't want the same value to be assigned to the sounds array. sounds[0],  sounds[1],  sounds[3] should have different values all the time. Thanks   string[] numbers ={ "1", "2", "3", "4", "5", "6", "7","8","9","10","11","12","13","14","15","16","17","18","19","20"};  Random r= new Random();  sounds[0] = numbers[r.Next(0, numbers.Count())];  sounds[1] = numbers[r.Next(0, numbers.Count())];  sounds[2] = numbers[r.Next(0, numbers.Count())];
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.
16473
Consider the class below that represents a Broker:  public class Broker {     public string Name = string.Empty;     public int Weight = 0;      public Broker(string n, int w)     {         this.Name = n;         this.Weight = w;     } }   I'd like to randomly select a Broker from an array, taking into account their weights.  What do you think of the code below?  class Program     {         private static Random _rnd = new Random();          public static Broker GetBroker(List&lt;Broker&gt; brokers, int totalWeight)         {             // totalWeight is the sum of all brokers' weight              int randomNumber = _rnd.Next(0, totalWeight);              Broker selectedBroker = null;             foreach (Broker broker in brokers)             {                 if (randomNumber &lt;= broker.Weight)                 {                     selectedBroker = broker;                     break;                 }                  randomNumber = randomNumber - broker.Weight;             }              return selectedBroker;         }           static void Main(string[] args)         {             List&lt;Broker&gt; brokers = new List&lt;Broker&gt;();             brokers.Add(new Broker("A", 10));             brokers.Add(new Broker("B", 20));             brokers.Add(new Broker("C", 20));             brokers.Add(new Broker("D", 10));              // total the weigth             int totalWeight = 0;             foreach (Broker broker in brokers)             {                 totalWeight += broker.Weight;             }              while (true)             {                 Dictionary&lt;string, int&gt; result = new Dictionary&lt;string, int&gt;();                  Broker selectedBroker = null;                  for (int i = 0; i &lt; 1000; i++)                 {                     selectedBroker = GetBroker(brokers, totalWeight);                     if (selectedBroker != null)                     {                         if (result.ContainsKey(selectedBroker.Name))                         {                             result[selectedBroker.Name] = result[selectedBroker.Name] + 1;                         }                         else                         {                             result.Add(selectedBroker.Name, 1);                         }                     }                 }                   Console.WriteLine("A\t\t" + result["A"]);                 Console.WriteLine("B\t\t" + result["B"]);                 Console.WriteLine("C\t\t" + result["C"]);                 Console.WriteLine("D\t\t" + result["D"]);                  result.Clear();                 Console.WriteLine();                 Console.ReadLine();             }         }     }   I'm not so confident. When I run this, Broker A always gets more hits than Broker D, and they have the same weight.  Is there a more accurate algorithm?  Thanks!
22800
I have two arrays like this:  A=(vol-175a3b54 vol-382c477b vol-8c027acf vol-93d6fed0 vol-71600106 vol-79f7970e vol-e3d6a894 vol-d9d6a8ae vol-8dbbc2fa vol-98c2bbef vol-ae7ed9e3 vol-5540e618 vol-9e3bbed3 vol-993bbed4 vol-a83bbee5 vol-ff52deb2) B=(vol-175a3b54 vol-e38d0c94 vol-2a19386a vol-b846c5cf vol-98c2bbef vol-7320102b vol-8f6226cc vol-27991850 vol-71600106 vol-615e1222)   The arrays are not sorted and might possibly even contain duplicated elements.   I would like to make the intersection of these two arrays and store the elements in another array. How would I do that? Also, how would I get the list of elements that appear in B and are not available in A?
6251
Choose a random number between $0$ and $1$ and record its value. Do this again and add the second number to the first number. Keep doing this until the sum of the numbers exceeds $1$. What's the expected value of the number of random numbers needed to accomplish this?
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
14769
I need to concatenate two String arrays in Java.  void f(String[] first, String[] second) {     String[] both = ??? }   What is the easiest way to do this?
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;     } }
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?
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));         }      }
67033
When answering , I wrote up this , which works fine from my machine. However, on SE data, it always picks the same winner. Is it possible that SE Data fixes rand() to a specific number?  If so, why is that?
37331
I have a coworker who writes unit tests for objects which fill their fields with random data. His reason is that it gives a wider range of testing, since it will test a lot of different values, whereas a normal test only uses a single static value.  I've given him a number of different reasons against this, the main ones being:   random values means the test isn't truly repeatable (which also means that if the test can randomly fail, it can do so on the build server and break the build) if it's a random value and the test fails, we need to a) fix the object and b) force ourselves to test for that value every time, so we know it works, but since it's random we don't know what the value was   Another coworker added:   If I am testing an exception, random values will not ensure that the test ends up in the expected state random data is used for flushing out a system and load testing, not for unit tests   Can anyone else add additional reasons I can give him to get him to stop doing this?  (Or alternately, is this an acceptable method of writing unit tests, and I and my other coworker are wrong?)
63948
Suppose three players play the following game: Player 1 picks a number in $[0,1]$. Then player $2$ picks a number in the same range but different from the number player $1$ picked. Player $3$ also picks a number in the same range but different from the previous two. We then pick a random number in $[0,1]$ uniformly randomly. Whoever has a number closer to the random number we picked wins the game. Assume all players play optimally with the goal of maximizing their probability of winning. If one of them has several optimal choices, they pick one of them at random.  1)If Player 1 chooses zero, what is the best choice for player 2?  2)What is the best choice for player 1?  I have some trouble seeing how this problem is well-defined. For instance, if Player $1$ picks $0$ and Player $2$ picks 1, then I cannot see what the optimal choice would be for the last player since he has to pick different numbers. Can someone help?  EDIT: I now understand better how the problem works, but I still have no idea how to approach this. Can someone give me some hints?
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?
103195
I read this  and in many shuffling and sorting algorithms you need to swap two items in a list or array. But what does a good and efficient Swap method look like?  Let's say for a T[] and for a List&lt;T&gt;. How would you best implement a method that swaps two items in those two?  Swap(ref cards[i], ref cards[n]);   // How is Swap implemented?
29117
So I was playing around the other day just to see exactly how mass assignment works in JavaScript.  First I tried this example in the console:  a = b = {}; a.foo = 'bar'; console.log(b.foo);   The result was "bar" being displayed in an alert. That is fair enough, a and b are really just aliases to the same object. Then I thought, how could I make this example simpler.  a = b = 'foo'; a = 'bar'; console.log(b);   That is pretty much the same thing, isn't it? Well this time, it returns foo not bar as I would expect from the behaviour of the first example.  Why does this happen?  N.B. This example could be simplified even more with the following code:    a = {}; b = a; a.foo = 'bar'; console.log(b.foo);  a = 'foo'; b = a; a = 'bar'; console.log(b);     (I suspect that JavaScript treats primitives such as strings and integers differently to hashes. Hashes return a pointer while "core" primitives return a copy of themselves)
6421
I have two arrays in PHP as follows:  People:  Array (     [0] =&gt; 3     [1] =&gt; 20 )   Wanted Criminals:  Array (     [0] =&gt; 2     [1] =&gt; 4     [2] =&gt; 8     [3] =&gt; 11     [4] =&gt; 12     [5] =&gt; 13     [6] =&gt; 14     [7] =&gt; 15     [8] =&gt; 16     [9] =&gt; 17     [10] =&gt; 18     [11] =&gt; 19     [12] =&gt; 20 )   How do I check if any of the People elements are in the Wanted Criminals array?  In this example, it should return true because 20 is in Wanted Criminals.
108260
So I have been tasked to make an array without duplicated values from another existing array. So I did it, but I want to know is there some other better way to do that. Example input/output: Input: 10, 15, 10, 5, 1, 3 Output: 10, 15, 5, 1, 3 So here is my code. #include &lt;stdio.h&gt;  int main(void) {   const int MAX_ARRAY_SIZE = 5;    int m[MAX_ARRAY_SIZE], p[MAX_ARRAY_SIZE];     for(int i = 0; i &lt; MAX_ARRAY_SIZE; i++) {     printf(&quot;Enter number: &quot;);     scanf(&quot;%d&quot;,&amp;m[i]);   }   int k = 0;   int dup = 0;   for(int i =0; i &lt; MAX_ARRAY_SIZE; i++) {     for(int j = i +1; j &lt;MAX_ARRAY_SIZE; j++) {         if(m[i] == m[j]) {             dup = 1;         }     }     if(dup != 1) {       p[k++] = m[i];     }     dup = 0;   }    printf(&quot;The new array without repeated values\n&quot;);   for(int i = 0; i &lt; k; i++) {     printf(&quot;%d\n&quot;,p[i]);   }    return 0; }  I'm not sure if this is the right and simple way I do that. I want some suggestions. Thanks in advance. :)