i have 7 teams and i want with using the class random and the next method(a,b), i want to create and save the results randomly.

i'll tell you what the program dowes so, you can understand what i want to do:
firstly, when the program runs, it will ask for a username and a password. Then, the user will give the dateConduct so, the randomly creation begin.

public class Game{

Other.Date dateConduct;
Team[] Teams;
Team winner;
bool conduct=false;

}

public class Team{

string name;
string captain;
int points;
}

Recommended Answers

All 13 Replies

So what exactly do you need help with?

i want you to tell me how i can create randomly results for the 7 teams. Using the randonm Class and the method Next(a,b)

Well, this is how to use the Random class and Next(int a, int b,)

Random rnd = new Random()

//Generates a random number between 0 and 10
points = rnd.Next(0,10);

So you could give the team a method which when called uses the above to generate a points score for them? Obviously changing the upper and lower bounds as you see fit.

MSDN: Here

thank you so much!

Why would you use Random class exactly? I dont see a point. To randomly shuffle teams between ?

From what I understand Mitja, it is used to randomly generate a points total for each of the seven teams specified, hence the team having an int points;

:)

But it has no point in this.
Points are (in my opinion) the points -for football (in europe):
3 - for winning
1- for equal
0 - for loosing

---

So really dont see the point in using Random, only in case if he want to shuffle teams in between to create, lets says, some kind of championship.
Who knows... :)

There was no mention of it being football related :)

^Exatly, what i want to do

You want it like football scores?

Could do this then for each "match fixture"

Random rnd = new Random();
int[] availablePoints = new int[3] { 0, 1, 3 }

points = availablePoints[rnd.Next(0,2)];

Giving a random result of either 0 1 or 3 when applied to the array :)

Edit: If no one beats me to it i'll make a match fixture thing tonight when back from work

Freedom - PLEASE next time you write into here, define EXACTLY what you wound want from this project to do. But I mean exactly - not "this is what I want it to do", or "yes, random scores".

We dont know now what to think, you saw me I have guessed what you had been thinking about.

Agree with Mitja, I'll write a bit of team code and it'll be up tonight regardless of what you want, gives me something to do :)

Its finally done, quite basic but shows the concepts of using the random class to create a tournament.

Points to note about the code below:
- Tournament plays only 20 games
- There is no validation in terms of all teams playing even amounts of matches
- Its very basic :)

Hopefully though it'll give you an idea on how the random class can be used.

namespace ScorerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Tournament theTournment = new Tournament();
            theTournment.PlayMatches();
            Console.WriteLine();
            theTournment.GetTournamentOutcome();
            Console.ReadLine();
        }
    }

    class Team : IComparable
    {
        private string teamName;
        private string teamCaptain;
        private int points;

        int IComparable.CompareTo(object obj)
        {
            Team team = (Team)obj;

            if (this.Points > team.Points)
                return -1;
            else
                if (this.Points == team.Points)
                    return 0;
                else
                    return 1;
        }

        public Team(string TeamName, string TeamCaptain)
        {
            teamName = TeamName;
            teamCaptain = TeamCaptain;
        }

        public void AddPoints(int PointsToAdd)
        {
            points += PointsToAdd;
        }

        public int Points { get { return points; } }
        public string TeamName { get { return teamName; } }
        public string TeamCaptain { get { return teamCaptain; } }

    }

    class Match
    {
        public Team[] teamsPlaying = new Team[2];
        public int pointsWon { get; private set; }

        private int[] possiblePoints = new int[3] { 0, 1, 3 };
        private Random rnd = new Random();

        public Match(Team HomeTeam, Team AwayTeam)
        {
            teamsPlaying[0] = HomeTeam;
            teamsPlaying[1] = AwayTeam;
        }

        public Team PlayMatch(out int PointsWon)
        {
            pointsWon = possiblePoints[rnd.Next(0,2)];

            PointsWon = pointsWon;
            return teamsPlaying[rnd.Next(0,1)];
        }
    }

    class Tournament
    {
        private Team[] tournamentCompetitors = new Team[10];
        private Match[] theMatches = new Match[20];
        private Random rnd = new Random();


        public Tournament()
        {
            SetUpTournament();
            SetUpMatches();
        }

        public void GetTournamentOutcome()
        {
            Team[] tournamentFinishPlaces = tournamentCompetitors;
            int position = 1;

            Array.Sort(tournamentFinishPlaces);

            foreach (Team team in tournamentFinishPlaces)
            {
                Console.WriteLine("{0}. {1} - {2} pts", position, team.TeamName, team.Points);
            }
        }

        public void PlayMatches()
        {
            foreach (Match match in theMatches)
            {
                int pointsWon;
                Team tempTeam = match.PlayMatch(out pointsWon);

                foreach (Team team in tournamentCompetitors)
                {
                    if (team.TeamName == tempTeam.TeamName && team.TeamCaptain == tempTeam.TeamCaptain)
                    {
                        team.AddPoints(pointsWon);
                    }
                }
            }
        }

        private void SetUpMatches()
        {
            for (int i = 0; i < 20; i++)
            {
                int teamOne;
                int teamTwo;

                teamOne = rnd.Next(0, 9);
                teamTwo = rnd.Next(0, 9);

                Match tempMatch = new Match(tournamentCompetitors[teamOne], tournamentCompetitors[teamTwo]);
                theMatches[i] = tempMatch;
            }
        }

        private void SetUpTournament()
        {
            Console.WriteLine("- - Tournament Setup - -");
            Console.WriteLine();

            for (int i = 0; i < 10; i++)
            {
                string teamName;
                string teamCaptain;

                Console.Write("Please enter Team {0} Name: ", i + 1);
                teamName = Console.ReadLine();
                Console.Write("Please enter Team Captain: ");
                teamCaptain = Console.ReadLine();
                Console.WriteLine();

                Team tempTeam = new Team(teamName, teamCaptain);
                tournamentCompetitors[i] = tempTeam;
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.