Jump to content

rearrange numbers randomly

- - - - -

  • Please log in to reply
2 replies to this topic

#1
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
Hi
I wrote this Program to rearrange 1,2,...,16 randomly:

using System;


class Program

{

    static Random ran = new Random();    

    static int[] Select()

    {        

        int[] a = {0,0,0,0, 0,0,0,0,  0,0,0,0,  0,0,0,0};

        int[] b = {1,1,1,1,  1,1,1,1,  1,1,1,1,  1,1,1,1};

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

        {

            a[i] = ran.Next(a.Length - i);

            for(int j = 0; j <= a[i]; j++)

            {

                if(b[j] == 0)

                    a[i]++;

            }            

            b[a[i]] = 0;

            a[i]++; // just because a[i] is in {0,...,15} and must be in {1,..,16} 

        }

        return a;

    }

    static void Main()

    {

        int[] a = Select();

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

            Console.WriteLine(a[i]);     

    /// test:

        /*int[] test = {0,0,0,0, 0,0,0,0,  0,0,0,0,  0,0,0,0};

        for(uint i = 0; i < 16000; i++)

        {

            int[] a = Select();

            test[a[6] - 1]++;                

        }

        for(uint i = 0; i < 16; i++)

            Console.WriteLine(test[i]);*/

        Console.ReadKey();

    }

}

Do you have any idea how to improve it. Is there anything wrong in my program?
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I think I did something similar to this, earlier this year (like in January, 2011, or around that time). The program I wrote made two lists, each having randomly-arranged integers from 3 through 12. Also, in each list, each integer could only be, and at least has to be, used once. Though I can't seem to find the code right now, but I used it back then for making a times table type of thing, for practicing multiplication (and no, I'm not a 3rd grader; I just wanted to get a little faster with practicing like that).

#3
Todilo

Todilo

    Newbie

  • Members
  • Pip
  • 5 posts
You want the numbers 1->16 in a list. This is how to do it for a List<int>, which of cause can be converted into an array if that is what is needed. Here is a way, it is not that much shorter but I think it is more readable:


   static void Main(string[] args)

        {

            List<int> numbers = new List<int>();

            List<int> randomNumbers = new List<int>();

            Random random = new Random();


            //Firstly, populate the list

            for (int i = 1; i <= 16; i++)

            {

                numbers.Add(i);

            }


            while (numbers.Count > 0)

            {

                int listIndex = random.Next(numbers.Count - 1);

                randomNumbers.Add(numbers[listIndex]);

                numbers.RemoveAt(listIndex);

            }



            foreach (int a in randomNumbers)

            {

                Console.WriteLine(a);

            }

            Console.ReadLine();

        }






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users