Jump to content

Randomize

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
noxrawr

noxrawr

    Newbie

  • Members
  • PipPip
  • 23 posts
Hi guys,

Ive got a simple script of which I want to randomize the order of the notation.

My enumeration:
enum Weather { cold, sunny, rainy }

Program:

static void Main(string[] args)

        {

            Weather weather = Weather.cold;

            Random r = new Random();

            

            string[] goh = new string[3];

            for (int i = 0; i <= 2; i++)

            {

                

                goh[i] = (weather.ToString());

                Console.WriteLine(goh[i]);

                weather++;

                

            }

Now, I DO know how I could generally randomize the process of the array elements assignments. But that is not exactly what I want. I'd like each weather, to be picked ONCE in a random order. So for example, sunny, rainy, cold. But how exactly do you let the system know that a particular weather type is already picked and should not be picked again, so you dont get something like sunny sunny, rainy.

Thanks in advance.

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
check this program out from the MSdn library,it uses bytes data type & generates different output for each iteration.
[COLOR=Blue]byte[/COLOR][] bytes1 = [COLOR=Blue]new[/COLOR] [COLOR=Blue]byte[/COLOR][100];
[COLOR=Blue]byte[/COLOR][] bytes2 = [COLOR=Blue]new[/COLOR] [COLOR=Blue]byte[/COLOR][100];
Random rnd1 = [COLOR=Blue]new[/COLOR] Random();
Random rnd2 = [COLOR=Blue]new[/COLOR] Random();

rnd1.NextBytes(bytes1);
rnd2.NextBytes(bytes2);

Console.WriteLine([COLOR=#a31515]"First Series:"[/COLOR]);
[COLOR=Blue]for[/COLOR] ([COLOR=Blue]int[/COLOR] ctr = bytes1.GetLowerBound(0); 
     ctr <= bytes1.GetUpperBound(0); 
     ctr++) { 
   Console.Write([COLOR=#a31515]"{0, 5}"[/COLOR], bytes1[ctr]);
   [COLOR=Blue]if[/COLOR] ((ctr + 1) % 10 == 0) Console.WriteLine();
} 
Console.WriteLine();
Console.WriteLine([COLOR=#a31515]"Second Series:"[/COLOR]);        
[COLOR=Blue]for[/COLOR] ([COLOR=Blue]int[/COLOR] ctr = bytes2.GetLowerBound(0);
     ctr <= bytes2.GetUpperBound(0);
     ctr++) {
   Console.Write([COLOR=#a31515]"{0, 5}"[/COLOR], bytes2[ctr]);
   [COLOR=Blue]if[/COLOR] ((ctr + 1) % 10 == 0) Console.WriteLine();
}   
[COLOR=Green]// The example displays the following output to the console:[/COLOR]
[COLOR=Green]//       First Series:[/COLOR]
[COLOR=Green]//          97  129  149   54   22  208  120  105   68  177[/COLOR]
[COLOR=Green]//         113  214   30  172   74  218  116  230   89   18[/COLOR]
[COLOR=Green]//          12  112  130  105  116  180  190  200  187  120[/COLOR]
[COLOR=Green]//           7  198  233  158   58   51   50  170   98   23[/COLOR]
[COLOR=Green]//          21    1  113   74  146  245   34  255   96   24[/COLOR]
[COLOR=Green]//         232  255   23    9  167  240  255   44  194   98[/COLOR]
[COLOR=Green]//          18  175  173  204  169  171  236  127  114   23[/COLOR]
[COLOR=Green]//         167  202  132   65  253   11  254   56  214  127[/COLOR]
[COLOR=Green]//         145  191  104  163  143    7  174  224  247   73[/COLOR]
[COLOR=Green]//          52    6  231  255    5  101   83  165  160  231[/COLOR]
[COLOR=Green]//       [/COLOR]
[COLOR=Green]//       Second Series:[/COLOR]
[COLOR=Green]//          97  129  149   54   22  208  120  105   68  177[/COLOR]
[COLOR=Green]//         113  214   30  172   74  218  116  230   89   18[/COLOR]
[COLOR=Green]//          12  112  130  105  116  180  190  200  187  120[/COLOR]
[COLOR=Green]//           7  198  233  158   58   51   50  170   98   23[/COLOR]
[COLOR=Green]//          21    1  113   74  146  245   34  255   96   24[/COLOR]
[COLOR=Green]//         232  255   23    9  167  240  255   44  194   98[/COLOR]
[COLOR=Green]//          18  175  173  204  169  171  236  127  114   23[/COLOR]
[COLOR=Green]//         167  202  132   65  253   11  254   56  214  127[/COLOR]
[COLOR=Green]//         145  191  104  163  143    7  174  224  247   73[/COLOR]
[COLOR=Green]//          52    6  231  255    5  101   83  165  160  231 [/COLOR]
second series is Identical to first series.this can be avoided making seed value time-dependent, thereby producing a different series with each new instance of Random.
read more here

Edited by gokuajmes, 31 May 2010 - 06:51 AM.
added info


#3
noxrawr

noxrawr

    Newbie

  • Members
  • PipPip
  • 23 posts
Like i said in my original post, i already know how to randomize stuff, but that wasnt the problem. So unless I misunderstood something you gave me an answer to something i did not ask.

Thanks anyway.

#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Random from 1 to 3. Depending one the number (1,2,3) use one of the weathers.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#5
noxrawr

noxrawr

    Newbie

  • Members
  • PipPip
  • 23 posts
static void Main(string[] args)
        {
            Weather weather = Weather.cold;
           
            Random r = new Random();
            
            
            string[] goh = new string[3];
            int checkone = 0;
            int checktwo = 0;
            int checkthree = 0;
            for (int i = 0; i <= 2; i++)
            {
                int number = r.Next(1, 4);
                
                weather = Weather.cold;
                if (number == 1 && checkone !=1)
                {
                    
                    {
                        Console.WriteLine(weather.ToString());
                        checkone = 1;
                    }
                    
                }

                if (number == 2 && checktwo != 1)
                {
                    
                    {
                        weather++;
                        Console.WriteLine(weather.ToString());
                        checktwo = 1;
                    }

                }

                if (number == 3 && checkthree !=1)
                {
                    
                    {
                        weather += 2;
                        Console.WriteLine(weather.ToString());
                        checkthree = 1;
                    }

                }

                }


can anyone please explain to me this:
So as you can see I want all 3 enums to be listed randomly (kinda shuffled) but the problem is that it is by no means robust and very glitchy.
I still dont quite understand how I can perform these checks with the 2 posts above therefor, could someone please enlighten me on how to actually make this robust using arrays?
Regards.

#6
semprance

semprance

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
At a glance, you could use a list and use the Contains method to check whether the List contains weather.toString()

#7
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
I had similar task when picking cards from the deck - you can't pick the same card twice.

This is the solution. I use an array of all possible values (three weathers in your case), and an indicator of number of remaining elements in the array. Once random element is picked, it is replaced with the last element from the array and count is reduced by one, so that next element is picked among one less amount. The process continues until counter reaches zero, meaning that all original elements were picked out.

Here's the code:


Random r = new Random();


// First populate the array with all candidates, i.e. all enumeration values

Array a = Enum.GetValues(typeof(Weather));

Weather[] weathers = new Weather[a.Length];

for (int i = 0; i < a.Length; i++)

    weathers[i] = (Weather)a.GetValue(i);


int count = weathers.Length;    // Indicates number of remaining values that were not printed


// Now consume enumeration values from the array until none remains

while (count > 0)

{

                

    int index = r.Next(count);

    Weather w = weathers[index];

    weathers[index] = weathers[--count];


    Console.WriteLine(Enum.GetName(typeof(Weather), w));


}