Random Object in F#

Introduction

This article explains and shows the use of the Random object. The sample shows how to change the background color of a Windows Forms form using a button click event. Color can be represented as ARGB (Alpha, Red, Green, Blue) values, that store the alpha (opaque) transparency, as well as the red, green and blue color values that each are in the range of 0 to 255.

Random Object

A Random class defined in the .NET framework class library provides the functionality to generate random numbers. A Random object has the ability to produce a sequence of numbers that meet certain statistical requirements for randomness.

The following are the methods of the Random class:

  • Random.Next( ): Returns a random number or nonnegative integer.
  • Random.Next(20) : Generates a random, nonnegative integer less than 20.
  • Random.NextBytes( ): Returns an array of bytes filled with a random number.
  • Random.Next(10,20): Genrates a random integer 10 or greater but less than 20.
  • Random.NextDoubles( ): NextDoubles returns a random number between 0.0 and 1.0.

Next() Method

The Next method has three overloaded forms that allows users to set the minimum and maximum range of the random number. After creating the instance of the Random class, you can start getting numbers through the Next method, that is overloaded in three versions. The Next method does not take any arguments. The syntax is :

  let objrandom = new Random()

  let red, green, blue = objrandom.Next(256), objrandom.Next(256), objrandom.Next(256)

 

Using the Random class, you can generate random positive numbers up to a maximum value. You can specify the minimum and the maximum range of numbers that the Next method works with. The first argument specifies the lowest value and the second argument specifies the highest value that the Next method can generate and operates between those two values.

 

  let objrandom = new Random()

  let ran = objrandom.Next(10,30)

Color.FromArgb( ) Method

Creates a color structure from the four color components (Alpha, Red, Green, Blue). The Color.FromArgb( ) method allows a 32-bit value to be passed for each component, the value of each component is limited to 8 bits. The Red, Green, Blue uses 8 bits each, that have integer values from 0 to 255. This makes 256*256*256=16777216 possible colors.

The FromArgb method allows you to specify the alpha channel, the FromRgb method uses a default value of 255 for the alpha channel, the alpha color determines the amount of transparency of the color. The alpha value 255 indicates that the color is completely opaque, and a value of 0 indicates the color is completely transparent.

  let objrandom = new Random()

  let red, green, blue = objrandom.Next(256), objrandom.Next(256), objrandom.Next(256)

  colorchngfrm.BackColor <- Color.FromArgb(red, green, blue) )

Use the following procedure to create a sample showing you how to change  the background color of a Windows Forms form with the click event of a button.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

CreateApplication

Step 2:

Now go to the Solution Explorer to the right side of the application. Right-click on "References" and select "Add references".

SelectReferences

 



AddReferences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Drawing" and "System.Windows.Forms" while holding down the Ctrl key and click on "Ok".

ImportNamespaces

Write the following code in the F# Application :

Step 4:

open System

open System.Drawing

open System.Windows.Forms

 

let colorchngfrm = new Form(Text = "Random Object")

let changebtn = new Button(Top=50,Left=40,Width=100)

let exitbtn=new Button(Text="Exit",Top=200,Left=40,Width=60)

changebtn.Text<-"Selcet Color"

changebtn.ForeColor<-Color.Black

changebtn.BackColor<-Color.Ivory

exitbtn.ForeColor<-Color.Black

exitbtn.BackColor<-Color.Ivory

colorchngfrm.Controls.Add(changebtn)

colorchngfrm.Controls.Add(exitbtn)

changebtn.Click.Add(fun _ ->

//create the instance of Random class

  let objrandom = new Random()

  let red, green, blue = objrandom.Next(256), objrandom.Next(256), objrandom.Next(256)

  colorchngfrm.BackColor <- Color.FromArgb(red, green, blue) )

exitbtn.Click.Add(fun _->

colorchngfrm.Close())

colorchngfrm.Show()

Application.Run(colorchngfrm)

 

The preceding program generates a new color randomly using the three colors (red, green and blue) from the range 0-255. The background color of the window is randomly changed while clicking on the button.

Step 1 :

Debug the application by pressing F5 to execute the Windows Application. After debugging the application the output will be as in the following figure:

AfterDebug

Step 2 :

Now click on the button "Select Color"" as in the figure given below.

Randomcolor1.jpg

Step 3 :

Now again click on the button "Select Color" to generate a new background color on the Windows Forms form as in the figure given below.

Randomcolor2.jpg

Step 4 :

Click on the button "Select Color" and a random color will be generated as in the figure given below.

Randomcolor3

Step 5 :

Randomcolor4

Summary

In this article you have seen how to change the background color of a Windows Forms form using Color.FromArgb.


Similar Articles