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 !!!
Decent post. Here's a device to use to construct your portable applications in minutes without coding.mobile app development // mobile app developers // iPhone app maker
ReplyDeleteI don't think why developers are working with random numbers they can develop their own code to
ReplyDeletedevelop their application.
For more details please visit
windows mobile app // iPhone application development // Android app maker
You have nice blog with great information, I always been for a new post from you, thanks for sharing such a good post about mobile app development
ReplyDeleteiphone app development company
We offer cross platform mobile solutions including SIP dialer to Windows Application Development Company worldwide clients. We specialize in the development of business apps for iOS, Android, Windows, and SmartPhones.
ReplyDeleteGreat information and Nice blog. Here is many post it is really helpful for me. Here is a device to use construct your portable applications in minutes without coding. Visit our website More info.
ReplyDeleteWonderful Information
ReplyDeleteMobile App Development Company in India
Mobile App Development Company in Mumbai
Android App Development Company in India
iOS App Development Company in India
Hybrid App Development company in India at
http://www.appslure.com/mobile-apps-development/
Mobile app Development Company in Dubai at
http://www.appslure.online
Mobile app development company in Kuwait at
http://www.appslure.online/mobile-app-development-company-kuwait.html
Mobile App Development Company in Abu Dhabi at
http://www.appslure.online/mobile-app-development-company-abu-dhabi-uae.html
Website Development company in Delhi at
http://www.appslure.com/web-development/
Mobile app Development Company in Delhi
http://www.appslure.com/
IOS app Development Company in Gurgaon,
IOS app Development Company in Noida at
http://www.appslure.com/ios-app-development-company
Android app Development Company in Gurgaon,
Android app Development Company in Noida at
http://www.appslure.com/android-app-development-company/
In our catalogue you can find the comprehensive range of Laboratory glassware available for sale. By making purchases at our online store, the customers may be sure of the excellent quality and the compatible prices for every unit. We do not work with any shady companies, tempting with cheapness of substandard products. All of our trustworthy suppliers consider customers’ satisfaction and delight as their top priority, just as we do.
ReplyDelete