There is a random number that sees numbers 0 or 1 . It is necessary between 0 и 1 add the block Angle_Left , and between 1 и 0 Angle_Top how to do?

 var Random = new Random(); for(int i=0;i<20;i++){ float rand = Random.Next(0,2); if(rand == 0){ Console.WriteLine(rand + " — " + "Left"); } if(rand == 1){ Console.WriteLine(rand + " — " + "Top"); } 

Example:

 0 - Left - Angle_Left 1 - Top 1 - Top 1 - Top - Angle_Top 0 - Left - Angle_Left 1 - Top - Angle_Top 0 - Left 0 - Left 0 - Left - Angle_Left 1 - Top 1 - Top 
  • 3
    Keep in memory the current value of Rand and the previous one. Before you draw a value based on the current Rand, check the previous one and write an intermediate message based on that. - iluxa1810
  • can you show by example code? - Kill Noise

1 answer 1

Well, for example:

 Random r = new Random(); IEnumerable<int> GetRandomSequence() { while (true) yield return r.Next(2); } IEnumerable<string> InsertCorners(IEnumerable<int> seq) { int prev = -1; foreach (var curr in seq) { if (prev == 0 && curr == 1) yield return " - Angle_Left"; else if (prev == 1 && curr == 0) yield return " - Angle_Top"; if (curr == 0) yield return "0 - Left"; else if (curr == 1) yield return "1 - Top"; prev = curr; } } 

Testing:

 foreach (var s in InsertCorners(GetRandomSequence().Take(20))) Console.WriteLine(s); 

gives out

 0 - Left 0 - Left 0 - Left 0 - Left - Angle_Left 1 - Top 1 - Top 1 - Top - Angle_Top 0 - Left 0 - Left - Angle_Left 1 - Top 1 - Top - Angle_Top 0 - Left 0 - Left - Angle_Left 1 - Top 1 - Top 1 - Top - Angle_Top 0 - Left 0 - Left 0 - Left - Angle_Left 1 - Top 
  • what is necessary, thank you very much)) and if you still have to do in the very beginning Angle_Left or Angle_Top or Left or Top ? - Kill Noise
  • @KillNoise: Didn't quite understand - which of the four? - VladD
  • the first line in the result can be 0 - Left or 1 - Top yes? you can also randomly type it on Angle_Left or Angle_Top But only the first line is Kill Noise
  • @KillNoise: Random row first line, 0 or 1. - VladD
  • @KillNoise: You can instead int prev = -1; make int prev = r.Next(2); , if you want to. This will add a possible angle. - VladD