SHARE
TWEET

Fisher-Yates shuffle - Random EE blocks placing

capasha Feb 27th, 2013 462 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace fillrandom
  7. {
  8.     class Program
  9.     {
  10.         static List<blocks> block = new List<blocks>();
  11.         static void Main(string[] args)
  12.         {
  13.             //Fill the list with blocks
  14.             for (int w = 0; w < 25; w++)
  15.             {
  16.                 for (int h = 0; h < 25; h++)
  17.                 {
  18.                     block.Add(new blocks() { bid = 9, x = w, y = h });
  19.                 }
  20.             }
  21.             //Shuffle the blocks
  22.             block.Shuffle<blocks>();
  23.             List<blocks> shuffled = new List<blocks>();
  24.             shuffled.AddRange(block);
  25.  
  26.             //Read the blocks and write them out
  27.             foreach (blocks bids in shuffled)
  28.             {
  29.                 Console.WriteLine(bids.x + " " + bids.y);
  30.             }
  31.             Console.ReadKey();
  32.         }
  33.     }
  34.  
  35.     class blocks {
  36.         public int x { get ; set; }
  37.         public int y { get ; set; }
  38.         public int bid { get ; set; }
  39.     }
  40.  
  41.     public static class Methods
  42.     {
  43.         public static void Shuffle<T>(this IList<T> list)
  44.         {
  45.             Random random = new Random();
  46.             int count = list.Count;
  47.             while (count > 1)
  48.             {
  49.                 count--;
  50.                 int num2 = random.Next(count + 1);
  51.                 T local = list[num2];
  52.                 list[num2] = list[count];
  53.                 list[count] = local;
  54.             }
  55.         }
  56.     }
  57.  
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top