Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. random numbers

random numbers

Scheduled Pinned Locked Moved C#
questiondata-structureshelplounge
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jad Jadallah
    wrote on last edited by
    #1

    i know this question is dull but i need to ask it guys. If i have an array of strings lets say ( string[] array = {"1","2","4","boss","678"} ) and i need to pick one randomly without a specific string lets say "4" (the answer should not never be 4). May i know how ? Thanks for the help, P.S: i can do it in different ways but i need it in one line of code and that, my friends , i dont know how. Any help appreciated .... Thanks guys

    J G 2 Replies Last reply
    0
    • J Jad Jadallah

      i know this question is dull but i need to ask it guys. If i have an array of strings lets say ( string[] array = {"1","2","4","boss","678"} ) and i need to pick one randomly without a specific string lets say "4" (the answer should not never be 4). May i know how ? Thanks for the help, P.S: i can do it in different ways but i need it in one line of code and that, my friends , i dont know how. Any help appreciated .... Thanks guys

      J Offline
      J Offline
      Jim Conigliaro
      wrote on last edited by
      #2

      I don't think your going to get it done in one line of code. If you are trying to keep your code simple, then I would write a routine that did the job and let calling it be your one line of code. So for example, I would write a function called GetRandomValue that handled all of the work, and then simply call that function when I needed it. private object GetRandomValue(object[] possibleValues, object illegalValue) {    Random randomIndexGenerator = new Random();    int index = randomIndexGenerator.Next(possibleValues.Length-1);    object randomValue = possibleValues[index];    while(object == illegalValue)    {       index = randomIndexGenerator.Next(possibleValues.Length-1);       randomValue = possibleValues[index];    }    return randomValue; }

      Jim Conigliaro jconigliaro@ieee.org
      http://www.jimconigliaro.com

      J 1 Reply Last reply
      0
      • J Jim Conigliaro

        I don't think your going to get it done in one line of code. If you are trying to keep your code simple, then I would write a routine that did the job and let calling it be your one line of code. So for example, I would write a function called GetRandomValue that handled all of the work, and then simply call that function when I needed it. private object GetRandomValue(object[] possibleValues, object illegalValue) {    Random randomIndexGenerator = new Random();    int index = randomIndexGenerator.Next(possibleValues.Length-1);    object randomValue = possibleValues[index];    while(object == illegalValue)    {       index = randomIndexGenerator.Next(possibleValues.Length-1);       randomValue = possibleValues[index];    }    return randomValue; }

        Jim Conigliaro jconigliaro@ieee.org
        http://www.jimconigliaro.com

        J Offline
        J Offline
        Jad Jadallah
        wrote on last edited by
        #3

        Thanks man it has been very helpful ... but what i am really looking for is that i need to pick a random number from array0 excluding a certain number. the new array(the old one excluding that value)is array1(array1= array0-x) and then i need to pick randomly from array2 which is array1 excluding a new variable y) so array2 = array1 - y = (array0 - x)-y and so on. hope i made myself clear. I wanna thank you again

        1 Reply Last reply
        0
        • J Jad Jadallah

          i know this question is dull but i need to ask it guys. If i have an array of strings lets say ( string[] array = {"1","2","4","boss","678"} ) and i need to pick one randomly without a specific string lets say "4" (the answer should not never be 4). May i know how ? Thanks for the help, P.S: i can do it in different ways but i need it in one line of code and that, my friends , i dont know how. Any help appreciated .... Thanks guys

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          If you explain what you are trying to accomplish, it might be possible to give a useful answer. It might answer some of those questions, for example: Is it the string value or the item in the array that is important? May the string value occur more than once in the array? Does the string value always occur at least once in the array? Why do you need this in one line of code? Why do you specify one line of code in a language that isn't line oriented? How complex may that line of code be?

          --- b { font-weight: normal; }

          J 1 Reply Last reply
          0
          • G Guffa

            If you explain what you are trying to accomplish, it might be possible to give a useful answer. It might answer some of those questions, for example: Is it the string value or the item in the array that is important? May the string value occur more than once in the array? Does the string value always occur at least once in the array? Why do you need this in one line of code? Why do you specify one line of code in a language that isn't line oriented? How complex may that line of code be?

            --- b { font-weight: normal; }

            J Offline
            J Offline
            Jad Jadallah
            wrote on last edited by
            #5

            okay i am gonna explain what i am trying to do... i am doing a poker game as a project.... we cant have the same card twice. i am putting strings in an array {"11","21"..."131","12","22"..."132"..."}(ex:"34"is the card 3 of clubs, 3 number, 4 kind) . Since we cant have same card twice i used a while loop on each card. but since the while loop is causing the application to freeze with CPU usage 100% i thought i can ,on each card, pick up the card randomly from the an new array which is the old array excluding the names of the cards already picked. Thanks again.

            G 2 Replies Last reply
            0
            • J Jad Jadallah

              okay i am gonna explain what i am trying to do... i am doing a poker game as a project.... we cant have the same card twice. i am putting strings in an array {"11","21"..."131","12","22"..."132"..."}(ex:"34"is the card 3 of clubs, 3 number, 4 kind) . Since we cant have same card twice i used a while loop on each card. but since the while loop is causing the application to freeze with CPU usage 100% i thought i can ,on each card, pick up the card randomly from the an new array which is the old array excluding the names of the cards already picked. Thanks again.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Use a list instead of an array, then you can remove the card from the list. Here is a bit more object oriented approach, instead of using strings... :)

              public enum Kind { Hearts, Clubs, Diamonds, Spades }

              public class Card {

              private Kind \_kind;
              private int \_value;
              
              public Card(Kind kind, int value) {
              	\_kind = kind;
              	\_value = value;
              }
              
              public Kind Kind { get { return \_kind; } }
              public int Value { get { return \_value; } }
              
              public string ValueString {
              	get {
              		switch (\_value) {
              			case 1: return "Ace";
              			case 11: return "Jack";
              			case 12: return "Queen";
              			case 13: return "King";
              			default: return \_value.ToString();
              		}
              	}
              }
              
              public override ToString() {
              	return this.ValueString + " of " + \_kind.ToString();
              }
              

              }

              public class Deck {

              private List<Card> \_cards;
              private Random \_rnd;
              
              public Deck() {
              	\_cards = new List<Card>(52);
              	for (int i = 1; i <= 13; i++) {
              		\_cards.Add(new Card(Kind.Hearts, i));
              		\_cards.Add(new Card(Kind.Clubs, i));
              		\_cards.Add(new Card(Kind.Diamonds, i));
              		\_cards.Add(new Card(Kind.Spades, i));
              	}
              	\_rnd = new Random();
              }
              
              public Card GetRandom() {
              	if (\_cards.Count == 0) throw new ApplicationException("Deck is empty.");
              	int pos = \_rnd.Next(\_cards.Count);
              	Card card = \_cards\[pos\];
              	\_cards.RemoveAt(pos);
              	return card;
              }
              

              }

              Example of usage:

              Deck deck = new Deck();
              List<Cards> hand = new List<Cards>();
              for (int i=0; i<5; i++) {
              hand.Add(deck.GetRandom());
              }
              for (int i=0; i<5; i++) {
              Console.WriteLine(hand[i].ToString());
              }

              Example output:

              King of Spades
              4 of Diamonds
              8 of Clubs
              Queen of Hearts
              Ace of Spaces

              Disclaimer: None of the code here is tested.

              --- b { font-weight: normal; }

              1 Reply Last reply
              0
              • J Jad Jadallah

                okay i am gonna explain what i am trying to do... i am doing a poker game as a project.... we cant have the same card twice. i am putting strings in an array {"11","21"..."131","12","22"..."132"..."}(ex:"34"is the card 3 of clubs, 3 number, 4 kind) . Since we cant have same card twice i used a while loop on each card. but since the while loop is causing the application to freeze with CPU usage 100% i thought i can ,on each card, pick up the card randomly from the an new array which is the old array excluding the names of the cards already picked. Thanks again.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Blackocebo wrote:

                Hello man, I was impressed by your posts and i really need your help. i am creating a card game ( some kind of poker) to give it to my friend in order to play it for free and no more spend money to play it. I have recently finished the game but i am facing a problem, when pressing the (DEAL) button sometimes, at some points, the application freezes. The CPU usage reaches 100%. So i want to ask you as an expert what should i do. [lengthy code not included]

                Well, let's look at a part of the code:

                num2 = randomnum.Next(1, 14); kind2 = randomnum.Next(1, 5); string string2 = num2.ToString() + kind2.ToString(); while (string2 == string1) { num2 = randomnum.Next(1, 14); kind2 = randomnum.Next(1, 5); }

                As you don't change any of the strings inside the loop, that you use in the condition, if you ever enter into the loop, you will never exit. The same of course applies to the three following loops too.

                --- b { font-weight: normal; }

                J 1 Reply Last reply
                0
                • G Guffa

                  Blackocebo wrote:

                  Hello man, I was impressed by your posts and i really need your help. i am creating a card game ( some kind of poker) to give it to my friend in order to play it for free and no more spend money to play it. I have recently finished the game but i am facing a problem, when pressing the (DEAL) button sometimes, at some points, the application freezes. The CPU usage reaches 100%. So i want to ask you as an expert what should i do. [lengthy code not included]

                  Well, let's look at a part of the code:

                  num2 = randomnum.Next(1, 14); kind2 = randomnum.Next(1, 5); string string2 = num2.ToString() + kind2.ToString(); while (string2 == string1) { num2 = randomnum.Next(1, 14); kind2 = randomnum.Next(1, 5); }

                  As you don't change any of the strings inside the loop, that you use in the condition, if you ever enter into the loop, you will never exit. The same of course applies to the three following loops too.

                  --- b { font-weight: normal; }

                  J Offline
                  J Offline
                  Jad Jadallah
                  wrote on last edited by
                  #8

                  I want to thank u very very much ... Respect

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups