Jump to content

how to get all the possible combination of the number i generate

- - - - -

  • Please log in to reply
4 replies to this topic

#1
nero29

nero29

    Newbie

  • Members
  • PipPip
  • 12 posts
guys help me i dont know what to do. heres the thing i created this window form right? then when you click this button it will generate random numbers and then the random numbers that has been generated will be listed on the listview with all the possible combinations it can get.

heres so far my code on my form1.cs
:

Quote

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace System7_12
{
public partial class Form1 : Form
{
public static ArrayList randomNums = new ArrayList();
public static int sysNum = 7;
public static Random rNum = new Random();

public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
randomNums.Clear();
CheckDuplication(randomNums);

//display random numbers...
foreach (int x in randomNums)
{
textBox1.Text += x + " ";
}

//permutation...
int combi = 0;
combi = (int)((permutation(sysNum)) / (permutation(6) * (permutation(sysNum - 6))));
MessageBox.Show(combi.ToString());

//display possible combinations...


for (int count = 1; count <= combi; count++)
{
//ArrayList sixNum =
// int count = 1;
// string combination = null;
// ArrayList display = new ArrayList();
// display = randomNums;
// display.RemoveAt(display.Count - count);
// foreach (int dsply in display)
// {
// combination += dsply.ToString() + " ";
// }
// listView1.Items.Add(count.ToString()).SubItems.Add(combination);

}
}
public int permutation(int x)
{
int num = 1;
for (int i = 1; i <= x; i++)
{
num *= i;
}
return num;
}
public void CheckDuplication(ArrayList randomNums)
{
bool change = true;
if (randomNums.Count < sysNum)
{
while (randomNums.Count < sysNum)
{
randomNums.Add(rNum.Next(1,43));
}
randomNums.Sort();
CheckDuplication(randomNums);
}
else if(randomNums.Count == sysNum)
{
for (int i = 0; i < randomNums.Count - 1; i++)
{
int plus = i + 1;
if ((int)randomNums[i] == (int)randomNums[plus]) randomNums.RemoveAt(i);
}
if (randomNums.Count == sysNum) change = false;
if(change == true) CheckDuplication(randomNums);
}
}
#region System Number radioButtons...
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true) sysNum = 7;
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked == true) sysNum = 8;
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked == true) sysNum = 9;
}

private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (radioButton4.Checked == true) sysNum = 10;
}

private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
if (radioButton5.Checked == true) sysNum = 11;
}

private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
if (radioButton6.Checked == true) sysNum = 12;
}
#endregion

}
}


#2
nero29

nero29

    Newbie

  • Members
  • PipPip
  • 12 posts
up please help meeee

#3
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
I'm not clear on what you mean by combinations. If your numbers were (1, 2, 3) do you mean (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2) and (3, 2, 1) (these are actually all the permutations) or do you actually mean the combinations (), (1), (2), (3), (1, 2), (1, 3), (2, 3), (1, 2, 3)? Or do you mean something completely different?

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Take a look at the following tutorial which explains the difference between permutations vs combinations and generates all permutations of a given string

http://forum.codecal...rmutations.html
Today is the first day of the rest of my life

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
If you are looking for C# code, you can check out Permutations or Combinations




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users