In: Computer Science
Create a random number generator object named myRandom and an integer variable named intRoulette. Set intRoulette to be a random number from 0 to 36 (including the numbers 0 and 36). (visual studios 2015) using tryparse
Below is the C# code where random number is generated in betwee to 36 (including the numbers 0 and 36).
Added comments in the code for better understanding.
using System;
class Program
{
static void Main()
{
//Create random number generated object myRandom
Random myRandom = new Random();
//int intRoulette variable
int intRoulette;
//myRandom.Next(0, 37) will generate random number in between 0-36.
//0 and 36 both inclusive. As max range is 37 so 37 will be included.
//out intRoulette will assign generated random number to intRoulette
//Int32.TryParse accepts first parameter as string so converting generated random number to string
//if tryParse succeeds then intRoulette will have correct value of random number between 0 and 36
if(Int32.TryParse(myRandom.Next(0, 37).ToString(), out intRoulette))
{
//Print the number set to intRoulette
Console.WriteLine("Random number set to intRoulette is : " +intRoulette);
}
Console.ReadLine();
}
}
Belos is the output screenshot