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
  1. Home
  2. General Programming
  3. C#
  4. just woundering about random numbers?

just woundering about random numbers?

Scheduled Pinned Locked Moved C#
questionlearningcsharpgame-devtutorial
10 Posts 5 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.
  • S Offline
    S Offline
    stephen darling
    wrote on last edited by
    #1

    Hello. I am learning c# and reading my book "Head first c#" In the interum, I would like to ask about generating random numbers. My example uses the game "Roulette" where the wheel spins, and a number between 0 and 36 comes out. I would like to ask the following questions: 1)What is the best (as in most random) way of generating a number between 0 and 36? 3)How would this be done if we had an animated roulette wheel, I ask this because if you play the game online, the ball may look as though it is ready to land on, say, 6, but then it appears to "bounce" to an adjacent number, is this down to a "random " value. And lastly, lets say we are creating a random horse race, but the horse in question has a 80% chance of winning, how would we add this "odds" into a random value? Thank you, Stephen

    D P L G 4 Replies Last reply
    0
    • S stephen darling

      Hello. I am learning c# and reading my book "Head first c#" In the interum, I would like to ask about generating random numbers. My example uses the game "Roulette" where the wheel spins, and a number between 0 and 36 comes out. I would like to ask the following questions: 1)What is the best (as in most random) way of generating a number between 0 and 36? 3)How would this be done if we had an animated roulette wheel, I ask this because if you play the game online, the ball may look as though it is ready to land on, say, 6, but then it appears to "bounce" to an adjacent number, is this down to a "random " value. And lastly, lets say we are creating a random horse race, but the horse in question has a 80% chance of winning, how would we add this "odds" into a random value? Thank you, Stephen

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Have a look at the Random[^] class.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      1 Reply Last reply
      0
      • S stephen darling

        Hello. I am learning c# and reading my book "Head first c#" In the interum, I would like to ask about generating random numbers. My example uses the game "Roulette" where the wheel spins, and a number between 0 and 36 comes out. I would like to ask the following questions: 1)What is the best (as in most random) way of generating a number between 0 and 36? 3)How would this be done if we had an animated roulette wheel, I ask this because if you play the game online, the ball may look as though it is ready to land on, say, 6, but then it appears to "bounce" to an adjacent number, is this down to a "random " value. And lastly, lets say we are creating a random horse race, but the horse in question has a 80% chance of winning, how would we add this "odds" into a random value? Thank you, Stephen

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        stephen.darling wrote:

        )What is the best (as in most random) way of generating a number between 0 and 36?

        With a roulette wheel. :-D As to the horse, he may be favored to win, but he doesn't have an "80% chance of winning". However, one of the techniques I like to use to select a random entity, from amomg various entities with varying chances of being selected, is to simulate putting slips into a hat, each entity may have a different number of slips in the hat, in proportion to their desired probability to be drawn.

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          stephen.darling wrote:

          )What is the best (as in most random) way of generating a number between 0 and 36?

          With a roulette wheel. :-D As to the horse, he may be favored to win, but he doesn't have an "80% chance of winning". However, one of the techniques I like to use to select a random entity, from amomg various entities with varying chances of being selected, is to simulate putting slips into a hat, each entity may have a different number of slips in the hat, in proportion to their desired probability to be drawn.

          S Offline
          S Offline
          stephen darling
          wrote on last edited by
          #4

          PIEBALDconsult wrote:

          With a roulette wheel. :-D

          Are you suggesting there is no real way of randomising this? :)

          PIEBALDconsult wrote:

          As to the horse, he may be favored to win, but he doesn't have an "80% chance of winning". However, one of the techniques I like to use to select a random entity, from amomg various entities with varying chances of being selected, is to simulate putting slips into a hat, each entity may have a different number of slips in the hat, in proportion to their desired probability to be drawn.

          Please could you explain this a little further? Thank you, Steve

          P 1 Reply Last reply
          0
          • S stephen darling

            PIEBALDconsult wrote:

            With a roulette wheel. :-D

            Are you suggesting there is no real way of randomising this? :)

            PIEBALDconsult wrote:

            As to the horse, he may be favored to win, but he doesn't have an "80% chance of winning". However, one of the techniques I like to use to select a random entity, from amomg various entities with varying chances of being selected, is to simulate putting slips into a hat, each entity may have a different number of slips in the hat, in proportion to their desired probability to be drawn.

            Please could you explain this a little further? Thank you, Steve

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            stephen.darling wrote:

            Please could you explain this a little further?

            Given the concept of drawing a winner from a hat... in many situations, each entrant has one entry in the hat, and therefore an equal chance of being drawn (winning)... but, in some situations entrants have unequal probability of being drawn... this can be accomplished by the entrants having varying numbers of entries in the hat... when an entry is drawn, the more entries an entrant has, the greater the probability of being drawn. If you need to continue drawing other entrants, you can remove all the entries for the drawn entrant before continuing. How this applies to horse racing, I don't know.

            1 Reply Last reply
            0
            • S stephen darling

              Hello. I am learning c# and reading my book "Head first c#" In the interum, I would like to ask about generating random numbers. My example uses the game "Roulette" where the wheel spins, and a number between 0 and 36 comes out. I would like to ask the following questions: 1)What is the best (as in most random) way of generating a number between 0 and 36? 3)How would this be done if we had an animated roulette wheel, I ask this because if you play the game online, the ball may look as though it is ready to land on, say, 6, but then it appears to "bounce" to an adjacent number, is this down to a "random " value. And lastly, lets say we are creating a random horse race, but the horse in question has a 80% chance of winning, how would we add this "odds" into a random value? Thank you, Stephen

              L Offline
              L Offline
              ljupcecar
              wrote on last edited by
              #6
              1. for random numbers

              static Random _r = new Random();
              static int F()
              {
              int n = _r.Next(0,36);
              return n;
              }

              1a) - if you want to make bingo card

              static void Main()
              {
              //if you want card with 6 numbers

                      int\[\] numbers = new int\[6\];
              
              
                  //to put different numbers at each place on the array
              
              
                      for (int i = 0; i < numbers.Length; i++)
                      {
                          int a = F();
                          bool has = numbers.Contains(a);
                          if (has == true) {i--; }
                          else {
                              numbers\[i\]=a;
                          }
                      }
              
              
                  //to sort it and print it
              
                      Array.Sort(numbers);
              
                      int j = 0;
                      foreach (int i in numbers)
                      {
                          Console.WriteLine("number at place " + (j+1).ToString() +" is " +i.ToString());
                          j++;
                      }
                      Console.ReadLine();
              
              
              }
              
                      static Random \_r = new Random();
                      static int F()
                      {
                          int n = \_r.Next(0,36);
                          return n;
                      }
              

              for horses and chances of winning

              static void Main(string[] args)
              {
              int a = F();

              //if random number wich is between 1 and 100 is smaler or equal 80 than horse win
              if (a <= 80)
              {
                  Console.Write("horse win!");
              }
              //else horse lose
              else { Console.Write("horse lose!"); }
              //and to check what is current value of number a - wich is number between 1 and 100
              Console.Write(" value of a is " + a.ToString());
              Console.ReadLine();
              

              }
              static Random _r = new Random();
              static int F()
              {
              int n = _r.Next(1, 100);
              return n;
              }

              1 Reply Last reply
              0
              • S stephen darling

                Hello. I am learning c# and reading my book "Head first c#" In the interum, I would like to ask about generating random numbers. My example uses the game "Roulette" where the wheel spins, and a number between 0 and 36 comes out. I would like to ask the following questions: 1)What is the best (as in most random) way of generating a number between 0 and 36? 3)How would this be done if we had an animated roulette wheel, I ask this because if you play the game online, the ball may look as though it is ready to land on, say, 6, but then it appears to "bounce" to an adjacent number, is this down to a "random " value. And lastly, lets say we are creating a random horse race, but the horse in question has a 80% chance of winning, how would we add this "odds" into a random value? Thank you, Stephen

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

                I'll take a bash at a slightly more realistic roulette simulator. The roulette wheel works by having the ball travel a spiral path at a random velocity in one direction and the wheel spin at a different random velocity in the opposite direction. The number is determined by the degree of rotation of the wheel at the time the ball reaches a point in the spiral at which it touches the wheel. Now, since i dont have the inclination to find the mathematical formula for a spiral we are going to assume that the ball is moving in decreasing circles. we are also going to run the ball and wheel in seperate threads, but i'll leave you to sort out the threading ;) Ball thread:

                public int RunAndReturnFinalAngle(int startRadius, int wheelRadius)
                {
                Random rng = new Random();
                int sampleCount = 360;
                int currentStep = 1;
                int delayTime = rng.Next(10, 100);
                int currentRadius = startRadius;
                int radiusReduction = rng.Next(1, 5);

                        double ballX = 0;
                        double ballY = 0;
                
                        int angle = 0;
                
                        while (currentRadius > wheelRadius)
                        {
                            currentStep++;
                            if(currentStep > sampleCount){currentStep = 1;}
                            
                            //angle of ball from vertical
                            angle = 360 \* (currentStep / sampleCount);
                
                            //coordinates of ball on circular trajectory
                            ballX = currentRadius \* Math.Sin(angle);
                            ballY = currentRadius \* Math.Cos(angle);
                
                            PositionBallGraphic(ballX, ballY);
                
                            //reduce trajectory radius by random ammount - Randomness element 1
                            currentRadius -= radiusReduction;
                            
                            //Sleep random time to give ball a random speed - Randomness element 2
                            Thread.Sleep(delayTime);
                        }
                
                        return angle;
                    }
                

                Now for the wheel, thats a lot simpler, simply run a loop modifying a value between 360 and 0 (remember its going the opposite direction) on a random delay (to set its speed) until the ball thread has done its thing. At this point you will have the final angle of the ball and the final angle of the wheel.

                public int GetNumberIndexForBall(int ballAngle, int wheelAngle)
                {
                int angleRelativeToWheel = ballAngle - wheelAngle;

                        //assume 36 numbers on wheel
                        return angleRelativeToWheel / 10;
                
                S 1 Reply Last reply
                0
                • G GParkings

                  I'll take a bash at a slightly more realistic roulette simulator. The roulette wheel works by having the ball travel a spiral path at a random velocity in one direction and the wheel spin at a different random velocity in the opposite direction. The number is determined by the degree of rotation of the wheel at the time the ball reaches a point in the spiral at which it touches the wheel. Now, since i dont have the inclination to find the mathematical formula for a spiral we are going to assume that the ball is moving in decreasing circles. we are also going to run the ball and wheel in seperate threads, but i'll leave you to sort out the threading ;) Ball thread:

                  public int RunAndReturnFinalAngle(int startRadius, int wheelRadius)
                  {
                  Random rng = new Random();
                  int sampleCount = 360;
                  int currentStep = 1;
                  int delayTime = rng.Next(10, 100);
                  int currentRadius = startRadius;
                  int radiusReduction = rng.Next(1, 5);

                          double ballX = 0;
                          double ballY = 0;
                  
                          int angle = 0;
                  
                          while (currentRadius > wheelRadius)
                          {
                              currentStep++;
                              if(currentStep > sampleCount){currentStep = 1;}
                              
                              //angle of ball from vertical
                              angle = 360 \* (currentStep / sampleCount);
                  
                              //coordinates of ball on circular trajectory
                              ballX = currentRadius \* Math.Sin(angle);
                              ballY = currentRadius \* Math.Cos(angle);
                  
                              PositionBallGraphic(ballX, ballY);
                  
                              //reduce trajectory radius by random ammount - Randomness element 1
                              currentRadius -= radiusReduction;
                              
                              //Sleep random time to give ball a random speed - Randomness element 2
                              Thread.Sleep(delayTime);
                          }
                  
                          return angle;
                      }
                  

                  Now for the wheel, thats a lot simpler, simply run a loop modifying a value between 360 and 0 (remember its going the opposite direction) on a random delay (to set its speed) until the ball thread has done its thing. At this point you will have the final angle of the ball and the final angle of the wheel.

                  public int GetNumberIndexForBall(int ballAngle, int wheelAngle)
                  {
                  int angleRelativeToWheel = ballAngle - wheelAngle;

                          //assume 36 numbers on wheel
                          return angleRelativeToWheel / 10;
                  
                  S Offline
                  S Offline
                  stephen darling
                  wrote on last edited by
                  #8

                  This sounds very interesting. I will continue to learn what I am from the book, then I may be able to place this code into context. Thank you, it seems much better than the standard Rand.next(0,35); Regards, Stephen

                  G 1 Reply Last reply
                  0
                  • S stephen darling

                    This sounds very interesting. I will continue to learn what I am from the book, then I may be able to place this code into context. Thank you, it seems much better than the standard Rand.next(0,35); Regards, Stephen

                    G Offline
                    G Offline
                    GParkings
                    wrote on last edited by
                    #9

                    if you don't need the ball placement then i would recommend sticking with the simple random.next(), A true random number is not possible (at least generated electronically) but the random.next() is pretty good and any system deriving a random number from logic wrapped around a call to that method is at best going to be equally as good but, more likely, introduce bias (which the code i posted above most certainly does). I posted that code more as a theoretical exercise in how one might simulate a (simplified) roulette wheel, for a system in which bias should be minimal (such as a gambling game in which real money is exchanging hands) it would be better to generate a random number through random.next() and then, having done this, calculate the animation of the ball (so that the ball itself is merely representing the already generated result and not playing a part in generating it) Keep reading the book, but don't forget to get in there and get your hands dirty. Nothing teaches programming like attempting to program ;)

                    S 1 Reply Last reply
                    0
                    • G GParkings

                      if you don't need the ball placement then i would recommend sticking with the simple random.next(), A true random number is not possible (at least generated electronically) but the random.next() is pretty good and any system deriving a random number from logic wrapped around a call to that method is at best going to be equally as good but, more likely, introduce bias (which the code i posted above most certainly does). I posted that code more as a theoretical exercise in how one might simulate a (simplified) roulette wheel, for a system in which bias should be minimal (such as a gambling game in which real money is exchanging hands) it would be better to generate a random number through random.next() and then, having done this, calculate the animation of the ball (so that the ball itself is merely representing the already generated result and not playing a part in generating it) Keep reading the book, but don't forget to get in there and get your hands dirty. Nothing teaches programming like attempting to program ;)

                      S Offline
                      S Offline
                      stephen darling
                      wrote on last edited by
                      #10

                      Hi. This is for fun, and not the real thing. It is going to be my first "real" project after the book. Although I would like it to be as "real" as possible. I was actually thinking along the same lines. 1) Generate a random number 0-36 2) Animate the wheel, and somehow when the ball arrives near the generated number, make it "bounce a little" as though it was going to (random again) land on the number to the left or right before "faling" into place. 3) Use some kind of list/enum to look up the value and also get whether it is black or red (or green) I have just started with the book now, and when I finish I am going to give this a real go. Thank you for your advice and comments. Kind Regards, Stephen

                      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