Home > Blockchain >  While (true) loop randomly stopps
While (true) loop randomly stopps

Time:02-15

First of all, I'm quite new to coding and I have to admit I've taken on quite a big task. So please be mindful that the code will not be written optimally at all. Ok, so my problem is that if I run my code in Visual Studio it just stops after some time. Could be after 30, 100, or more iterations. It just stops when it feels like it.

I've read some different threads but none of them seem to answer my question. The thing is: I don't use any Threads, I don't use "break" or anything because the program isn't supposed to stop.

Now the actual use of the program isn't in question it is just supposed to let me learn some new stuff. It is supposed to play my version of Roulette with 2 different input variables, then take the one with the better results, randomize those results a bit and go again and again and again until I exit out of the program. Here's the code (If you have questions tell me, it is quite confusing and long I have to admit. Suggestions and improvements are also highly welcome):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automation_for_Le_Algorythm
{
class Program
{
    static void Main(string[] args)
    {
        int generation = 1;
        
        float total = 15f;
        float black = 7f;
        float red = 7f;
        float green = 1f;

        float cBetsize;
        float cConfidence;
        float cBetsizemp;
        float cWhentomp;

        
        bool sndrun = false;
        bool rounddone = false;


        float chanceRed = red / total;
        float chanceBlack = black / total;
        float chanceGreen = green / total;

        float startingBal = 100;
        float gameBal = 100;
        float startbetSize = 0.5f;
        float beforeincrease = 0.5f;

        float expectedGreen = chanceGreen;
        float expectedRed = chanceRed;
        float expectedBlack = chanceBlack;
        float gamessinceGreenPredict = 0;
        
        float diff;

        

        List<int> set1 = new List<int>();
        List<int> set2 = new List<int>();

        string lastRound;
        string s = "None";


        Console.WriteLine("Enter starting balance");
        startingBal = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter starting bet size ( Will be divided by 10 )");
        startbetSize = Convert.ToInt32(Console.ReadLine()) / 10f;

        Console.WriteLine("Enter rounds per generation");
        int rounds = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Do you wish to proceed? (y/n)");
        string proceed = Console.ReadLine();
        if (proceed != "y")
        {
            Environment.Exit(0);
        }
        Console.WriteLine("\n Running...\n");

        float betsize1 = startbetSize;
        float confidence1 = 0.65f;
        float betsizemp1 = 4f;
        float whentomp1 = 14;

        float betsize2 = startbetSize;
        float confidence2 = 0.65f;
        float betsizemp2 = 4f;
        float whentomp2 = 14;



        while (true)
        {
            Random rand = new Random();

            betsize1 = betsize1   rand.Next(-1, 1);
          //confidence1 = confidence1   (rand.Next(-2, 2) / 100f); //Let's not randomise that...
            betsizemp1 = betsizemp1   (rand.Next(-5, 5) / 10f);
            whentomp1 = whentomp1   rand.Next(-1, 1);

            betsize2 = betsize2   rand.Next(-1, 1);
         //confidence2 = confidence2   (rand.Next(-2, 2) / 100f); //Let's not randomise that...
            betsizemp2 = betsizemp2   (rand.Next(-5, 5) / 10f);
            whentomp2 = whentomp2   rand.Next(-1, 1);

            if (betsize1 <= 0)
            {
                betsize1 = betsize1   1;
            }
            if (betsizemp1 <= 0)
            {
                betsizemp1 = betsizemp1   1;
            }
            if (whentomp1 <= 0)
            {
                whentomp1 = whentomp1   1;
            }

            if (betsize2 <= 0)
            {
                betsize2 = betsize2   1;
            }
            if (betsizemp2 <= 0)
            {
                betsizemp2 = betsizemp2   1;
            }
            if (whentomp2 <= 0)
            {
                whentomp2 = whentomp2   1;
            }
                for (int i = 0; i < rounds; i  )
            {
               // Console.WriteLine(1);  //Debug
               // System.Threading.Thread.Sleep(500); //Debug
            secondrun:
               // Console.WriteLine(3); //Debug
               // System.Threading.Thread.Sleep(500); //Debug
                if (sndrun == false)
                {
                    cBetsize = betsize1;
                    cConfidence = confidence1;
                    cBetsizemp = betsizemp1;
                    cWhentomp = whentomp1;

                }



                else
                {
                    cBetsize = betsize2;
                    cConfidence = confidence2;
                    cBetsizemp = betsizemp2;
                    cWhentomp = whentomp2;

                }
               
               
                
                
                
                gameBal = startingBal;
                s = "None";
                expectedBlack = 7f / 15f;
                expectedRed = 7f / 15f;
                expectedGreen = 1f / 15f;


            notfinished:
                //Console.WriteLine(2   "\n"   gameBal   " \n"); //Debug
                //Console.WriteLine(gamessinceGreenPredict); //Debug
                //System.Threading.Thread.Sleep(1); //Debug

                int pickedSlot = rand.Next(0, 16);

                if (pickedSlot != 15)
                {
                    if (pickedSlot <= 7)
                    {
                        lastRound = "r";
                    }
                    else
                    {
                        lastRound = "b";
                    }
                }
                else
                {
                    lastRound = "g";
                }

                if (gamessinceGreenPredict >=cBetsize * cWhentomp)
                {
                    beforeincrease = cBetsize;
                    cBetsize = cBetsize * cBetsizemp;
                }

                if (expectedGreen > expectedRed)
                {
                    if (expectedGreen > expectedBlack)
                    {
                        if (expectedGreen > cConfidence) // Wie wahrscheinlich muss es sein?
                        {
                            s = "Green";
                            gamessinceGreenPredict  ;
                        }

                        else
                        {
                            s = "None";
                        }

                    }
                }

                else if (expectedGreen > expectedBlack)
                {
                    if (expectedGreen > expectedRed)
                    {

                        if (expectedGreen > cConfidence) // Wie wahrscheinlich muss es sein?
                        {
                            s = "Green";
                            gamessinceGreenPredict  ;

                        }

                        else
                        {
                            s = "None";
                        }

                    }
                }


                if (expectedGreen < 0.65f)
                {
                    s = "None";
                }


                if (lastRound == "r")
                {
                    diff = expectedRed;
                    expectedRed = expectedRed * chanceRed;

                    diff = diff - expectedRed;
                    expectedBlack = expectedBlack   (6f / 7f) * diff;
                    expectedGreen = expectedGreen   (1f / 7f) * diff;

                    if (s == "Green")
                    {
                        gameBal = gameBal - cBetsize;
                    }
                }

                else if (lastRound == "b")
                {
                    diff = expectedBlack;
                    expectedBlack = expectedBlack * chanceBlack;

                    diff = diff - expectedBlack;
                    expectedRed = expectedRed   (6f / 7f) * diff;
                    expectedGreen = expectedGreen   (1f / 7f) * diff;

                    if (s == "Green")
                    {
                        gameBal = gameBal - cBetsize;
                    }
                }

                else if (lastRound == "g")
                {
                    diff = expectedGreen;
                    expectedGreen = expectedGreen * chanceGreen;

                    diff = diff - expectedGreen;
                    expectedBlack = expectedBlack   (1f / 2f) * diff;
                    expectedRed = expectedRed   (1f / 2f) * diff;

                    gamessinceGreenPredict = 0;

                    if (s == "Green")
                    {
                        gameBal = gameBal   (cBetsize * 14);
                        cBetsize = beforeincrease;
                    }
                }



                /*  foreach (var item in set1.ToArray()) // DEBUG
                  {

                      Console.WriteLine(item   ",");
                  }
                  foreach (var item in set2.ToArray())
                  {

                      Console.WriteLine(item   ",");
                  }
                  Console.WriteLine("\n");
                  Console.WriteLine(lastRound);
                  Console.WriteLine(gameBal);
                  Console.WriteLine(Convert.ToString(expectedGreen));
                  Console.WriteLine(cBetsize   "Betsize");
                  Console.WriteLine(cBetsizemp   "Mp");
                  Console.WriteLine("\n");
                  System.Threading.Thread.Sleep(20); // DEBUG
               */

                if (gameBal >= 300)
                {
                    
                    if (sndrun == false)
                    {
                        set1.Add(1);
                    }
                    else
                    {
                        set2.Add(1);
                        sndrun = false;
                        if (rounddone == true)
                        {
                            goto done;
                        }
                        rounddone = true;
                    }

                    
                    sndrun = true;
                    goto secondrun;
                }

                else if (gameBal <= 0)
                {
                   /* Console.WriteLine(gameBal); //Debug
                    Console.WriteLine(cBetsize); //Debug
                    Console.WriteLine(cBetsizemp); //Debug
                    Console.WriteLine(gamessinceGreenPredict   "\n"); //Debug
                   */
                    if (sndrun == false)
                    {
                        set1.Add(0);
                    }
                    else
                    {
                        set2.Add(0);
                        sndrun = false;
                        if (rounddone == true)
                        {
                            goto done;
                        }
                        rounddone = true;
                    }


                    sndrun = true;
                    
                    goto secondrun;
                }

                else
                {
                    goto notfinished;
                }
            done:;
                rounddone = false;
                //Console.WriteLine("Finished a Round"); //Can be annoying
               



            }
           // Console.WriteLine(5); //Debug
            float length1 = set1.ToArray().Length;
            float length2 = set2.ToArray().Length;
           
            set1.RemoveAll(i => i == 0);
            set2.RemoveAll(i => i == 0);
            
            
            float gottogoal1 = set1.ToArray().Length;
            float gottogoal2 = set2.ToArray().Length;
           
            float score1 = gottogoal1 / length1;
            float score2 = gottogoal2 / length2;
           

            
            if (score1 >= score2)
            {
                score2 = score1;
                betsize2 = betsize1;
                confidence2 = confidence1;
                betsizemp2 = betsizemp1;
                whentomp2 = whentomp1;
            }
            else
            {
                score1 = score2;
                betsize1 = betsize2;
                confidence1 = confidence2;
                betsizemp1 = betsizemp2;
                whentomp1 = whentomp2;
            }
            
            
            Console.WriteLine("\n  Generation "   generation   " achieved "   (score1 * 100)   "% !\n");
            generation  ;
            set1.Clear();
            set2.Clear();




        }

       










        }

        
    
}

}

CodePudding user response:

(before the question gets closed) Here are some general tips that apply to many questions that are similar in nature. In no particular order:

  • Reduce code to the required minimum to reproduce the faulty behavior. Reducing might already help you discover the problem yourself
  • Use a debugger to set breakpoints, single-step a program, and inspect variable values
  • Avoid goto, unless absolutely necessary
  • Remove any commented code, it only distracts from the actual code
  • Use proper formatting (indentation, following accepted naming guidelines, add empty lines at the appropriate places)
  • Split the problem into smaller sub-problems and solve each sub-problem first. Once all sub-problems are solved, combine them to solve the bigger problem
  • Keep complexity of methods low. If a method becomes too long and has too many branches (conditions, loops), split it into smaller methods
  • Take a pen and paper to sketch out your algorithm first
  • Print or log values of variables at specific places
  • Related