Another brain block... Random Numbers

This is a discussion on Another brain block... Random Numbers within the C# Programming forums, part of the General Programming Boards category; My problem now is trying to generate a random number, assign it to an array, then generate a new number, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    Another brain block... Random Numbers

    My problem now is trying to generate a random number, assign it to an array, then generate a new number, check it's not the same as the previous, or any other previous generated numbers, and if it isnt, assign it to the next array number, otherwise generate a new one.

    ie. -

    generate random number 1
    assign to array [1]
    generate random number 2
    check random number 2 is not the same as array[any previous number done]
    if same, regenerate and check again
    if not same, assign to array[next value]

    here is my code right now:

    Code:
    public void createMap()
    {
    Random rnd = new Random();
    Random rnd2 = new Random();
    Random rnd3 = new Random();
    Random rnd4 = new Random();
    Random rnd5 = new Random();
    Random rnd6 = new Random();
    string abctext = "";
    int abcde = 0;
    int dgfh = 0;
    for(int uvw = 0; uvw <= 24;uvw++)
    {
    for(int xyz = 0; xyz <= 35;xyz++)
    {
    int lmn = rnd.Next(3);
    if(lmn == 0 )
    {
    if(dgfh < 600)
    {
    if(xyz == 0 && uvw == 0)
    {
    maplocation[uvw,xyz,0] = rnd2.Next(100,599);
    
    }
    else
    {
    if(uvw == 0 && xyz >= 1)
    {
    for(int rst = 0; rst <= xyz-1;rst++)
    {
    while(maplocation[uvw,xyz,0] == maplocation[uvw,rst,0] && dgfh <= 600)
    {
    int randsysnum2 = new int();
    randsysnum2 = rnd3.Next(100,599);
    maplocation[uvw,xyz,0] = randsysnum2;
    }
    }
    }
    else
    {
    if(uvw >= 1 && xyz == 0)
    {
    for(int opq = 0; opq <= uvw-1;opq++)
    {
    while(maplocation[uvw,xyz,0] == maplocation[opq,xyz,0] && dgfh <= 600)
    {
    int randsysnum3 = new int();
    randsysnum3 = rnd4.Next(100,599);
    maplocation[uvw,xyz,0] = randsysnum3;
    }
    }
    }
    else
    {
    for(int opq = 0; opq <= uvw-1;opq++)
    {
    for(int rst = 0; rst <= xyz-1;rst++)
    {
    while(maplocation[uvw,xyz,0] == maplocation[opq,rst,0] && dgfh <= 600)
    {
    int randsysnum4 = new int();
    randsysnum4 = rnd6.Next(100,599);
    maplocation[uvw,xyz,0] = randsysnum4;
    }
    }
    }
    }
    }
    }
    }
    }
    else
    {
    maplocation[uvw,xyz,0] = 0;
    }
    dgfh++;
    myArray[abcde] = Convert.ToString(maplocation[uvw,xyz,0]);
    abcde++;
    }
    
    }
    Array.Sort(myArray);
    FileInfo fi = new FileInfo("test.txt");
    fi.Delete();
    StreamWriter w = File.AppendText("test.txt");
    for(int abcdef = 0; abcdef <= abcde-1; abcdef++)
    {
    abctext = abcdef+1 +" " +myArray[abcdef];
    Log (abctext, w);
    }
    w.Close();
    
    }
    Any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Sheesh, what a waste of time trying to get that written. So much simpler now!

    I solved it myself. I realised I wanted a lottery type situation, so I looked around for that, and then came up with this as my new code, which works perfectly!

    Code:
    bool exists = true;
    for(int uvw = 0; uvw <= 24;uvw++)
    {
    for(int xyz = 0; xyz <= 35;xyz++)
    {
    int lmn = rnd.Next(3);
    if(lmn == 0 )
    {
    maplocation[uvw,xyz,0] = rnd2.Next(100,599);
    exists = mapLocCount.Contains(maplocation[uvw,xyz,0]);
    while(exists == true)
    {
    int randsysnum2 = new int();
    randsysnum2 = rnd3.Next(100,599);
    maplocation[uvw,xyz,0] = randsysnum2;
    exists = mapLocCount.Contains(maplocation[uvw,xyz,0]);
    }
    mapLocCount.Push(maplocation[uvw,xyz,0]);
    }
    else
    {
    maplocation[uvw,xyz,0] = 0;
    }
    }
    
    }
    Just had to use a stack to help out.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Now for the next problem!!

    This is a weird one... Basically, what this code does, is it creates a map, for a game.

    it generates the stars in the game, then the next stage adds the starting positions, setting the subsystem types (planets or asteroids) and setting the parent star to the same type (yellow dwarf). Then it continues on and assigns the remaining stars their types.

    Now the code example below does that almost perfectly. A little tweaking, or a different way of placing the stars may be needed, but thats for later. The problem is, the second subsystem created in the first system selected to be a starting position only, is an 'unknown' type. it doesnt apply a proper system type to it. At least my coding practices work for something, that bit I planned to check for problems like this, except, I can't fix the problem!

    This code is one heck of a lot of if statements and switches. It is very conditional, and uses a few randoms too.

    I've marked the main bit of code to look at, the rest is there to let you see if the problem does come from my other functions.

    Code:
    public void createMap()
    		{
    			bool exists = true;
    			for(int uvw = 0; uvw <= 24;uvw++)
    			{
    				for(int xyz = 0; xyz <= 35;xyz++)
    				{
    					int lmn = rnd.Next(13);
    					if(lmn == 0 || lmn == 1 || lmn == 3 || lmn == 4)
    					{
    						maplocation[uvw,xyz,0] = rnd2.Next(100,599);
    						exists = mapLocCount.Contains(maplocation[uvw,xyz,0]);
    						while(exists == true)
    						{
    							int randsysnum2 = new int();
    							randsysnum2 = rnd3.Next(100,599);
    							maplocation[uvw,xyz,0] = randsysnum2;
    							exists = mapLocCount.Contains(maplocation[uvw,xyz,0]);
    						}
    						mapLocCount.Push(maplocation[uvw,xyz,0]);
    					}
    					else
    					{
    						maplocation[uvw,xyz,0] = 0;
    					}
    				}
    				
    			}
    		}
    		
    ** HERE ********public void createStartPlaces()
    		{
    			bool exists = true;
    			//int fgh;
    			for(int bcd = 1; bcd <= 12; bcd++)
    			{
    				switch(bcd)
    				{
    					case 1:
    						uvw = 1;
    						xyz = 1;
    						break;
    					case 2:
    						xyz = 10;
    						break;
    					case 3:
    						xyz = 18;
    						break;
    					case 4:
    						xyz = 27;
    						break;
    					case 5:
    						uvw = 9;
    						xyz = 1;
    						break;
    					case 6:
    						xyz = 10;
    						break;
    					case 7:
    						xyz = 18;
    						break;
    					case 8:
    						xyz = 27;
    						break;
    					case 9:
    						uvw = 17;
    						xyz = 1;
    						break;
    					case 10:
    						xyz = 10;
    						break;
    					case 11:
    						xyz = 18;
    						break;
    					case 12:
    						xyz = 27;
    						break;
    				}
    			
    				random = rnd.Next(uvw,uvw+6);
    				random2 = rnd2.Next(xyz,xyz+6);
    				// finds a suitable system
    				while(maplocation[random,random2,0] == 0)
    				{
    					random = rnd.Next(uvw,uvw+6);
    					random2 = rnd2.Next(xyz,xyz+6);
    				}
    				// sets the number of systems to 4
    				//systemType[maplocation[random,random2,1]] = 1;
    				maplocation[random,random2,1] = 4;
    				// starts a loop to add subsystems
    				for(efg = 2; efg <= 4;efg++)
    				{
    					if(efg == 2)
    					{
    						yhg = 0;
    					}
    					//generates a number
    					maplocation[random,random2,efg] = rnd3.Next(100,599);
    					//checks if this system number already exists
    					exists = mapLocCount.Contains(maplocation[random,random2,efg]);
    					//if it does, repeat the generation until it doesnt exist in the list
    					while(exists == true)
    					{
    						maplocation[random,random2,efg] = rnd4.Next(100,599);
    						exists = mapLocCount.Contains(maplocation[random,random2,efg]);
    					}
    					//assigns the subsystem number to the appropriate adj system number of the parent system
    					maplocation[random,random2,efg+4] = maplocation[random,random2,efg];
    					//add the number to the list
    					mapLocCount.Push(maplocation[random,random2,efg]);
    					//sets the first subsystem to be a terran planet, no matter what
    					if(efg == 2)
    					{
    						systemType[maplocation[random,random2,efg]] = 13;
    						pqr[random,random2] = "o";
    					}
    					else//allows the other subsystems to be martian venusian or jovian
    					{
    						if(efg == 4)
    						{
    							if(yhg == 0)
    							{
    								rhu = 19;
    								systemType[maplocation[random,random2,efg]] = rhu;
    							}
    							else
    							{
    								if(yhg >= 2)
    								{
    									rhu = rnd2.Next(16,18);
    									systemType[maplocation[random,random2,efg]] = rhu;
    								}
    								else
    								{
    									rhu = rnd2.Next(16,21);
    									systemType[maplocation[random,random2,efg]] = rhu;
    								}
    							}
    						}
    						else
    						{
    							rhu = rnd2.Next(16,22);
    							systemType[maplocation[random,random2,efg]] = rhu;
    						}
    					}
    					//assigns the character for the map generator
    					switch(rhu)
    					{
    						case 13:
    							pqr[random,random2] = pqr[random,random2] + "o";
    							systemTypeText[maplocation[random,random2,efg]] = "   Terran Planet";
    							break;
    						case 16:
    							pqr[random,random2] = pqr[random,random2] + "o";
    							systemTypeText[maplocation[random,random2,efg]] = "   Venusian Planet";
    							break;
    						case 17:
    							pqr[random,random2] = pqr[random,random2] + "o";
    							systemTypeText[maplocation[random,random2,efg]] = "   Martian Planet";
    							break;
    						case 18:
    							pqr[random,random2] = pqr[random,random2] + "o";
    							systemTypeText[maplocation[random,random2,efg]] = "   Jovian Planet";
    							break;
    						case 19:
    							pqr[random,random2] = pqr[random,random2] + "*";
    							systemTypeText[maplocation[random,random2,efg]] = "   Asteroids";
    							yhg++;
    							break;
    						case 20:
    							pqr[random,random2] = pqr[random,random2] + "*";
    							systemTypeText[maplocation[random,random2,efg]] = "   Asteroids";
    							yhg++;
    							break;
    						case 21:
    							pqr[random,random2] = pqr[random,random2] + "*";
    							systemTypeText[maplocation[random,random2,efg]] = "   Asteroids";
    							yhg++;
    							break;
    						case 22:
    							pqr[random,random2] = pqr[random,random2] + "*";
    							systemTypeText[maplocation[random,random2,efg]] = "   Asteroids";
    							yhg++;
    							break;
    						default:
    							pqr[random,random2] = pqr[random,random2] + "+";
    							systemTypeText[maplocation[random,random2,efg]] = "   ----------";
    							break;
    					}
    				}
    			}
    		}
    		
    		public void setStarTypes()
    		{
    			for(int uvw = 0; uvw <= 24; uvw++)
    			{
    				for(int xyz = 0; xyz <= 35;xyz++)
    				{
    					if(maplocation[uvw,xyz,0] > 0)
    					{
    						if(maplocation[uvw,xyz,1] == 4)
    						{
    							systemType[maplocation[uvw,xyz,0]] = 1;
    							systemTypeText[maplocation[uvw,xyz,0]] = "   Yellow Dwarf";
    						}
    						else
    						{
    							int rth = rnd2.Next(1,20);
    							systemType[maplocation[uvw,xyz,0]] = rth;
    						}
    					}
    				}
    			}
    		}
    		public void moreSubSystems()
    		{
    		}
    		public void systemLinks()
    		{
    		}
    		public void drawMap1()
    		{
    			string[] line1 = new string[25];
    			string[] line2 = new string[25];
    			string line3 = "\u000a";
    			string hjg;
    			string fdi = "";
    			string wok = "";
    			for( int uvw = 0 ; uvw <= 24 ; uvw++ )
    			{
    				for( int xyz = 0 ; xyz <= 35 ; xyz++ )
    				{
    					switch(systemType[maplocation[uvw,xyz,0]])
    					{
    						case 1:
    							wok = "Y";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Yellow Dwarf";
    							break;
    						case 2:
    							wok = "Y";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Yellow Dwarf";
    							break;
    						case 3:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 4:
    							wok = "R";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Dwarf";
    							break;
    						case 5:
    							wok = "G";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Giant";
    							break;
    						case 6:
    							wok = "G";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Giant";
    							break;
    						case 7:
    							wok = "G";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Giant";
    							break;
    						case 8:
    							wok = "G";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Giant";
    							break;
    						case 9:
    							wok = "W";
    							systemTypeText[maplocation[uvw,xyz,0]] = "White Dwarf";
    							break;
    						case 10:
    							wok = "B";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Black Dwarf";
    							break;
    						case 11:
    							wok = "L";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Red Variable";
    							break;
    						case 12:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 13:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 14:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 15:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 16:
    							wok = "S";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Super Giant";
    							break;
    						case 17:
    							wok = "V";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Blue Variable";
    							break;
    						case 18:
    							wok = "C";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Binary System";
    							break;
    						case 19:
    							wok = "N";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Neutron Star";
    							break;
    						case 20:
    							wok = "H";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Black Hole";
    							break;
    						default:
    							wok = "Q";
    							systemTypeText[maplocation[uvw,xyz,0]] = "Unknown";
    							break;
    					}
    					if(xyz == 35)
    					{
    						hjg = "\u000a";
    					}
    					else
    					{
    						hjg = "  ";
    					}
    					if(maplocation[uvw,xyz,0] == 0)
    					{
    						line1[uvw] = line1[uvw] +"    " +hjg;
    					}
    					else
    					{
    						line1[uvw] = line1[uvw] +maplocation[uvw,xyz,0] +wok +hjg;
    					}
    					
    					switch(maplocation[uvw,xyz,1])
    					{
    						case 0:
    							fdi = "    ";
    							break;
    						case 1:
    							fdi = "   ";
    							break;
    						case 2:
    							fdi = "  ";
    							break;
    						case 3:
    							fdi = " ";
    							break;
    						case 4:
    							fdi = "";
    							break;
    					}
    					line2[uvw] = line2[uvw] +pqr[uvw,xyz] +fdi +hjg;
    				}
    			}
    			
    			for(int ijh = 0;ijh <=24;ijh++)
    			{
    				lmn = lmn +line1[ijh] +line2[ijh] +line3;
    			}
    			maplabel.Text = lmn;
    		}
    		public void writeToTextFile()
    		{
    			Stack agh = new Stack();
    			int rdf = mapLocCount.Count;
    			object[] sig = mapLocCount.ToArray();
    			Array.Sort(sig);
    			FileInfo fi = new FileInfo("test.txt");
    			fi.Delete();
    			StreamWriter w = new StreamWriter("test.txt");
    			w.WriteLine(mapLocCount.Count);
    			w.WriteLine(" ");
    			for(int wji = 0; wji <= rdf-1;wji++)
    			{
    				w.WriteLine(Convert.ToString(sig[wji]) +" " +systemType[(int)sig[wji]] +" " +systemTypeText[(int)sig[wji]]);
    			}
    			w.Close();
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 01:56 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21