Thursday, 1 August 2013

Working with Random numbers in Windows Phone

Working with Random Numbers in Windows Phone

random number in c#

 

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> 

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);
        }
    }
}
So when you execute this application you will get the output like below

windows phone app developmentwindows phone app development



We are giving the complete source code which you can download and use directly 

Download Source Code
 
Hope You Enjoyed the Post !!!

7 comments :

  1. 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

    ReplyDelete
  2. I don't think why developers are working with random numbers they can develop their own code to
    develop their application.

    For more details please visit

    windows mobile app // iPhone application development // Android app maker

    ReplyDelete
  3. 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
    iphone app development company

    ReplyDelete
  4. 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.

    ReplyDelete
  5. Great 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.

    ReplyDelete
  6. 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