---
title: "How to generate Random Number in c#?"  
description: "How to generate Random Number in c#?"  
author: "Anonymous User"  
published: 2014-10-26  
updated: 2014-10-26  
canonical: https://www.mindstick.com/forum/2435/how-to-generate-random-number-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# How to generate Random Number in c#?

Hi Can [anyone help me](https://www.mindstick.com/forum/331/can-anyone-help-me-out-how-to-use-session-concept), i [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) the build a deck of Crads the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) i am having is producing [enough](https://answers.mindstick.com/qa/48601/who-is-the-writer-of-the-one-life-is-not-enough) [random numbers](https://www.mindstick.com/forum/832/generate-random-numbers-uniformly-over-entire-range) to fill the deck i get to 16 cards and then it seems i am not producing the remaining cards i need to fill the deck. **[code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) is below.**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TexasHoldEmTest{    public enum Suit { Clubs = 1, Diamonds = 2, Hearts = 3, Spades = 4 }    public enum CardNumber { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13 }    public class Card    {        public Suit suit;        public CardNumber number;        public bool isUsed = false;        public Card()        {            Thread.Sleep(1);            Random suit1 = new Random();            Random number1 = new Random();            suit = (Suit)suit1.Next(1, 5);            number = (CardNumber)number1.Next(1, 14);        }                public void showCard(List<Card> c)        {            Console.WriteLine("{0} of {1}",number, suit);        }                public void Deck(List<Card> c)        {            do            {                Card card = new Card();                bool exists = c.Exists(x => x.suit == card.suit && x.number == card.number);                if (exists == false)                {                    c.Add(card);                    Console.WriteLine("Generating Card {0}", c.Count);                }            }                       while (c.Count < 52);        }    }    class Game    {        static void Main(string[] args)        {            Card card = new Card();            List<Card> deckOfCards = new List<Card>();            card.Deck(deckOfCards);            foreach (Card c in deckOfCards)            {                c.showCard(deckOfCards);            }            Console.ReadLine();        }    }}
```

## Replies

### Reply by Anonymous User

To avoid repetitions, just use a single [Random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) object declared as a static field rather than a local variable.

If you make the changes shown, then all should be well:

\

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TexasHoldEmTest{    public enum Suit { Clubs = 1, Diamonds = 2, Hearts = 3, Spades = 4 }    public enum CardNumber { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13 }    public class Card    {        private static Random rand = new Random();         public Suit suit;        public CardNumber number;        public bool isUsed = false;        public Card()        {            suit = (Suit)rand.Next(1, 5);            number = (CardNumber)rand.Next(1, 14);        }                public void showCard(List<Card> c)        {            Console.WriteLine("{0} of {1}",number, suit);                    }                public void Deck(List<Card> c)        {            do            {                Card card = new Card();                bool exists = c.Exists(x => x.suit == card.suit && x.number == card.number);                if (exists == false)                {                    c.Add(card);                    Console.WriteLine("Generating Card {0}", c.Count);                }            }                       while (c.Count < 52);        }    }    class Game    {        static void Main(string[] args)        {            Card card = new Card();            List<Card> deckOfCards = new List<Card>();            card.Deck(deckOfCards);            foreach (Card c in deckOfCards)            {                c.showCard(deckOfCards);            }            Console.ReadLine();        }    }}
```


---

Original Source: https://www.mindstick.com/forum/2435/how-to-generate-random-number-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
