Working with Random Numbers in Windows Phone
For developing a game like application or developing a Quiz application, many people try to generate a Random number with their own algorithm.
But Windows Phone has a very useful way to generate the Random number in minutes. Even you can specify boundary for the Random number to be generated (E.g number should between 0 to 10).
here is the simple way to generate the Random number
Random myRandom=new Random();
int randomNumber=myRandom.Next(0,100);
so we have to create an object for Random class first and then we have to specify the boundary by Next method.
myRandom.Next(0,100) which generates the number between 0 to 99 (but not 100) and we have to store it in a integer variable
I created a sample application which generate random number every time a button event is clicked.
Project Name: RandomNumberSample
MainPage.xaml
<phone:PhoneApplicationPage
x:Class="RandomNumberSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls; assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Height="93" HorizontalAlignment="Left" Margin="69,81,0,0" Name="textBlock1" Text="Random Number" VerticalAlignment="Top" FontSize="48" />
<TextBlock Height="80" HorizontalAlignment="Left" Margin="141,265,0,0" Name="textBlock2" Text="" VerticalAlignment="Top" FontSize="56" />
<Button Content="Generate !!!" Height="72" HorizontalAlignment="Left" Margin="141,437,0,0" Name="button1" VerticalAlignment="Top" Width="214" Click="button1_Click" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="176,158,0,0" Name="textBlock3" Text="between 1 to 100" VerticalAlignment="Top" />
</Grid>
</phone:PhoneApplicationPage>
x:Class="RandomNumberSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls; assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Height="93" HorizontalAlignment="Left" Margin="69,81,0,0" Name="textBlock1" Text="Random Number" VerticalAlignment="Top" FontSize="48" />
<TextBlock Height="80" HorizontalAlignment="Left" Margin="141,265,0,0" Name="textBlock2" Text="" VerticalAlignment="Top" FontSize="56" />
<Button Content="Generate !!!" Height="72" HorizontalAlignment="Left" Margin="141,437,0,0" Name="button1" VerticalAlignment="Top" Width="214" Click="button1_Click" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="176,158,0,0" Name="textBlock3" Text="between 1 to 100" VerticalAlignment="Top" />
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace RandomNumberSample
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Random myRandom = new Random();
int randomNumber = myRandom.Next(0, 100);
textBlock2.Text = Convert.ToString(randomNumber);
} }
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace RandomNumberSample
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Random myRandom = new Random();
int randomNumber = myRandom.Next(0, 100);
textBlock2.Text = Convert.ToString(randomNumber);
} }
}
So when you execute this application you will get the output like below
Download Source Code
Hope You Enjoyed the Post !!!