0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace genetic_2._0
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public int totalfitness;
    public int counter2 = 0;
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void TotalFitness(int[] fitnesses)
    {
        foreach (int i in fitnesses)
        {
            totalfitness = totalfitness + i;
        }
    }
    int[] GetIntArray(int num)
    {
        List<int> listOfInts = new List<int>();
        while (num > 0)
        {
            listOfInts.Add(num % 10);
            num = num / 10;
        }
        listOfInts.Reverse();
        return listOfInts.ToArray();
    }
    private void button1_Click(object sender, EventArgs e)

    {
        genome[] genome = new genome[1000];
        genome[counter2] = new genome();
        genome[counter2].SetLenght(Int32.Parse((textBox1.Text)));
        int[] genes = GetIntArray(Int32.Parse(textBox2.Text));
        genome[counter2].SetGenesBinary(genes);
        genome[counter2].SetName("g"+counter2.ToString());
        genome[counter2].ConvertBinaryToDecimal();
        genome[counter2].GetFitness(genome[counter2].genes_decimal);
        string toDisplay = string.Join(Environment.NewLine, genome[counter2].genes_binary);
        string toDisplay2 = string.Join(Environment.NewLine, genome[counter2].genes_decimal);
         listBox1.Items.Add(genome[counter2].name1 + "                    " + genome[counter2].lenght1+"                 "+toDisplay+"                                    "+toDisplay2+ "                            "+genome[counter2].fitness);
        totalfitness = Int32.Parse(label4.Text);
        totalfitness = Int32.Parse(label4.Text) + genome[counter2].fitness;
        label4.Text = totalfitness.ToString();
        counter2++;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        genome[] genome = new genome[1000];
        int number = 0;
        do
        {
            genome[counter2] = new genome();
            genome[counter2].SetLenght(Int32.Parse((textBox1.Text)));
            genome[counter2].SetGenesBinaryAutomatic();
            genome[counter2].SetName("g" + counter2.ToString());
            genome[counter2].ConvertBinaryToDecimal();
            genome[counter2].GetFitness(genome[counter2].genes_decimal);
            string toDisplay = string.Join(Environment.NewLine, genome[counter2].genes_binary);
            string toDisplay2 = string.Join(Environment.NewLine, genome[counter2].genes_decimal);
            listBox1.Items.Add(genome[counter2].name1 + "                    " + genome[counter2].lenght1 + "                 " + toDisplay + "                                    " + toDisplay2 + "                            " + genome[counter2].fitness);
            totalfitness = Int32.Parse(label4.Text);
            totalfitness = Int32.Parse(label4.Text) + genome[counter2].fitness;
            label4.Text = totalfitness.ToString();
            counter2++;
            number++;
        } while (number < Int32.Parse(textBox3.Text));
        }
    }
}

public class genome

{
    public string name1; //nazwa
    public int lenght1; //długość genomu
    public int fitness;//fitness     
    public int[] genes_binary; //geny binarne
    public int[] genes_decimal= new int[2]; //geny decymalne
    public int chance; //chance of choseing
    int counter; //counter not to use//
public genome()
    {

    }
    public void SetName(string name)
    {
        name1 = name;
    } //ustaw nazwe
    public void SetLenght(int lenght)
    {
        lenght1 = lenght;
        genes_binary = new int[lenght];
    } //ustaw dlugosc
    public void SetGenesBinary(int[] genesbinary) //ustaw geny binarne
    {
        if(genesbinary.Length == lenght1)
        {
            counter = 0;
            foreach(int gene in genesbinary)
            {
                genes_binary[counter] = gene;
                counter++;
            }
        }
    }
    public void SetGenesBinaryAutomatic()
    {
    Random rand = new Random();

    counter = 0;
        int lenght;
        for (lenght = 0; lenght < lenght1; lenght++ )
        {
            genes_binary[counter] = rand.Next(0, 2);
        counter++;

        }

    }//ustaw geny binarne automatycznie
    public void ConvertBinaryToDecimal()//zmien geny binarne na decymalne
    { 
        if(genes_binary.Length%2 == 0 && genes_binary.Length <10)
        {
            //liczba parzysta mniejsza od dziesięciu
            string liczba = null;
            int polowa = (genes_binary.Length / 2);
            foreach (int i in genes_binary.Take(polowa))
            {
                string i_s = i.ToString();
                liczba = liczba + i_s;

            }
            genes_decimal[0] = Convert.ToInt32(liczba,2);
            liczba = null;
            foreach(int i in  genes_binary.Reverse().Take(polowa).Reverse())
            {
                string i_s = i.ToString();
                liczba = liczba + i_s;
            }
            genes_decimal[1] = Convert.ToInt32(liczba, 2);
        }
       else
        {
            string liczba = null;
            int b = genes_binary.Length / 2; //3 = 1
            int c = genes_binary.Length / 2 + 1;//3 = 2
            foreach (int i in genes_binary.Take(b))
            {
                string i_s = i.ToString();
                liczba = liczba + i_s;

            }
            genes_decimal[0] = Convert.ToInt32(liczba, 2);
            liczba = null;
            foreach (int i in genes_binary.Reverse().Take(c).Reverse())
            {
                string i_s = i.ToString();
                liczba = liczba + i_s;
            }
            genes_decimal[1] = Convert.ToInt32(liczba, 2);
        }
    }
    public void GetFitness(int[] genesdecimal)
    {
        int liczba = 0;
        foreach(int i in genes_decimal)
        {
            fitness = fitness + i;
        }

Now the button2 is for generating x random genes.We have function genome[x].Setgenesbinaryautomatic which is generating random genes in array. ex: [1,0,1,0]. But now in loop it is giving the same genes. How can I repair it so it will give random not the same genes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    You need to make your random generator global and then only call *.next()* where you need it. – Rob Aug 07 '17 at 12:54
  • 3
    You are creating your `Random` in a loop. So the seed for the random is mostly always the same. And due to the way pseudo random algorithms works, you will almost always get the same number. The easiest fix would be to declare your `Random` in the class itself and initialise it in either the constructor or at load . Here is an interesting read for you https://en.wikipedia.org/wiki/Pseudorandom_number_generator#Periodicity – litelite Aug 07 '17 at 12:54
  • In class there is a function SetGenesBinaryAutomatic which is generating random numbers. I've tried now changing position of Random rand = new random() to top of class and making it public but it is still giving the same arrays of numbers – Boże Bożenka Aug 07 '17 at 13:00
  • Ok now it's working.Thanks litelite for link it explains everything :D – Boże Bożenka Aug 07 '17 at 13:04

0 Answers0