Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic
Article

Random Ranged Number Generator

Rate me:
Please Sign up or sign in to vote.
3.41/5 (13 votes)
1 Jun 2011CPOL5 min read 22.8K   6   9
The objective of the project is to have an efficient program, by eliminating the repetitiveness of generated numbers as much as possible

Important Point

In my algorithm I actually used the system time (Seconds and Milliseconds) with the factorial function to generate the random numbers unlike other articles who haven’t mentioned the time technique in its generating process.

Introduction

Random number generators are used for encryption, gambling, and other applications however, a random number is a number generated by a process, where the output is unpredictable.

In fact there are two ways of generating numbers physical and software generators, where the software generated numbers are called pseudo random numbers. In my project, I used VB.NET to generate random numbers. The objective of the project is to:

  • Have an efficient program, by eliminating the repetitiveness of generated numbers as much as possible.
  • Ensure that the generated numbers are distributed over a defined set, which is from (1-1000).

Algorithm

The algorithm I implemented to generate random numbers provides a set of random numbers distributed among the range of (1-1000).

When the user runs the program, he/she can enter the numbers he/she wants to generate from (1-20) simply by clicking on the Generate button, where the minimum is 1 and the maximum is 20.

image001.png

For example, if the user wants to generate 100 random numbers, he/she will enter five times the number 20 consequently, and the output will contain 20 random numbers each time it is generated and is saved in a text file “C://File.txt”.

image002.png

By entering more than 20 numbers as an input, a message box will prompt the user: “Please enter a number less than or equal to 20”.

image003.png

The idea of generating random numbers, is when the user clicks on the Generate button, the algorithm will generate the first number by taking the minutes of the current time, adding it to the seconds, adding it to the milliseconds, then adding it to the factorial of the counter for the first number, which is 1. Then it will take the value of the result mod (1000), and after that, adds 1 to the final result.

VB
Dim strdate As New String("")
strdate = (((DateTime.Now.Minute) + (DateTime.Now.Second) + _
   (DateTime.Now.Millisecond) + Factorial(counter)) Mod (1000)) + (1)

While it's generating numbers, the idea of adding the factorial is as the follows: for example, the user wants to generate 20 random numbers. The first number is generated as we mentioned previously by taking the minutes of the current time, adding it to the seconds, adding the milliseconds, and adding the factorial of 1, then when generating the second number, after adding minutes, seconds, and milliseconds, and the factorial of 2, then for the third number, the factorial of 3 will be added and so on, until the 20 numbers are generated.

The idea of taking the total result (minutes added to seconds added to milliseconds, then added to the factorial of the counter) mod (1000) is to insure that the output of generating random numbers will not contain any number larger than 1000, so all the results will be in the range of (1-1000).

According to the algorithm, for more efficient results, number 1 will be added to the total result for each generated number. If the total result equals to zero, adding number 1 to the total result will ensure that the generated number will be in the range of (1-1000). If the total result equals to 999, adding number 1 will ensure that the set of generated random numbers will contain number 1000 according to the specified range.

After the calculation is performed, the result (strdate) will be converted into int64 saved in a text file “C://File.txt”.

When the generator finishes generating the first number, the generator will stop and sleep for 2 milliseconds before it starts generating the following random number, and so on for all numbers. This is used for eliminating the chance of repetitiveness in generating a large set of random numbers.

VB
If txtHowMany.Text > 20 Then
    MsgBox("Please Enter a number less than or equal to 20", MsgBoxStyle.Critical)
Else
    Dim counter As Integer = 1
    For counter = 1 To txtHowMany.Text
        Dim strdate As New String("")
        strdate = (((DateTime.Now.Minute) + (DateTime.Now.Second) + _
                   (DateTime.Now.Millisecond) +Factorial(counter)) Mod (1000)) + (1)
        strdate = strdate.Replace("/", "")
        Dim x As Int64
        x = Convert.ToInt64(strdate)
        Dim objWriter As New StreamWriter("C://File.txt", True)
        objWriter.WriteLine(x)
        Thread.Sleep(200)
        objWriter.Close()

Testing

When I tested the logic, the results where:

  • When I generated 50 random numbers, there were no duplicated values.
985, 190,397, 618, 917, 719, 243, 726, 489, 612, 814, 818, 
221, 824, 827, 29, 233, 436, 639, 842, 46, 250, 457, 678, 977, 
780, 303, 786, 549, 672, 875, 878, 281, 884, 887, 90, 293, 496, 
699, 902, 133, 337, 544, 765, 64, 867, 390, 873, 636,759.
  • When I generated 60 random numbers, there was one duplicate value.
844, 48, 254, 473, 770, 571, 93, 578, 341, 465, 666, 668, 70, 
671, 672, 875, 79, 280, 481, 682, 619, 821, 26, 246, 543, 344, 869, 352, 116, 
238, 352,116,238, 439, 441, 842,444,447, 650,853,54, 256, 457, 623, 825, 30, 
250,548, 356, 879, 360, 122, 243, 444, 445,850,454, 455, 656, 857, 58, 260,461.
  • When I generated 100 random numbers, there were two duplicate values.
598, 802, 7, 227, 528, 331, 853, 335, 97, 218, 420, 421,
825, 429,430, 631, 832, 33, 235, 440, 66, 268, 473, 699, 998, 
800, 322, 803, 564, 685, 890, 893, 296, 898, 899, 101, 302, 503, 
707, 910, 804, 7, 213, 432, 729, 538, 61, 544, 306, 427, 628, 629, 
31, 636, 639,842, 43, 245, 446, 647, 229, 439, 646, 867, 164, 966, 552, 
34, 795, 917, 120, 121, 523, 124,126, 327, 532, 734, 937, 140, 820, 22, 
234, 455, 752,553, 74,556, 318, 443, 646, 649, 51, 652, 653, 854, 55, 261, 464, 667.
  • When I generated 600 random numbers, there were 139 duplicate values.

Here is a sample set of 30 numbers from the generated result: 797, 1000, 208, 429, 728, 531, 53, 537, 300, 423, 626, 628, 32, 635, 638, 841, 300, 43, 247, 450, 653, 531, 467, 674, 895, 194,997, 520, 626, 3.

  • When I generated 1000 random numbers, there were 350 duplicate values.

A sample set of 30 numbers from the generated result: 97, 301, 600, 508, 729, 27, 831, 354, 837,600, 12, 926, 929, 332, 935, 141, 344, 547, 12, 651, 444, 88, 980, 515, 444, 240, 885, 666, 980, 331.

Data Analysis

After testing the different data sets, I have concluded the following:

  • After several repetitions for the first generated set which is in the range of (1-50), the percentage of repeated numbers was zero; this indicates that there is no number duplication.
  • For the next generated set which is (1-60), the percentage of repeated numbers was 1.6%; this means that generating till 50 numbers, the result will not contain any duplicate value.
  • For the generated set which is (1-100), the percentage of repeated numbers was 2%; this indicates that when generating 100 random numbers, there were two duplicate values. From my point of view, as a result, a 2% of repeated numbers among 100 numbers is efficient.
  • For the generated set which is (1-600), the percentage of repeated numbers was 23.1%; this indicates that when generating 600 random numbers, there were 139 duplicated values.
  • For the generated set which is (1-1000), the percentage of repeated numbers was 35%, this indicates that when generating 1000 random numbers, there were 350 duplicated values.
  • From my point of view, as a result, a 35% of repeated numbers among 1000 numbers is efficient, and still it’s less than the threshold 50%.
  • The results that were conducted are normally distributed among the range of (1-1000); they are not concentrated in a certain range.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Optimiza
Iraq Iraq
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontwo-digit numbers Pin
Amir Garajedaghi19-Oct-16 8:53
Amir Garajedaghi19-Oct-16 8:53 
GeneralMy vote of 3 Pin
skv_lviv6-Jun-11 18:33
skv_lviv6-Jun-11 18:33 
GeneralRe: My vote of 3 Pin
Haitham Alany6-Jun-11 21:09
Haitham Alany6-Jun-11 21:09 
General[My vote of 2] Reinventing the wheel PinPopular
Dave Brauns3-Jun-11 6:49
Dave Brauns3-Jun-11 6:49 
GeneralRe: [My vote of 2] Reinventing the wheel Pin
Haitham Alany4-Jun-11 23:31
Haitham Alany4-Jun-11 23:31 
GeneralRe: [My vote of 2] Reinventing the wheel Pin
hakkietakkie17-Jul-11 23:57
hakkietakkie17-Jul-11 23:57 
GeneralMy vote of 2 PinPopular
phi10102-Jun-11 6:38
phi10102-Jun-11 6:38 
GeneralMy vote of 2 PinPopular
Dr.Walt Fair, PE1-Jun-11 16:20
professionalDr.Walt Fair, PE1-Jun-11 16:20 
First, you don't say what type of random distribution you are trying to generate, except in the last sentence where you indicate they are normally distributed. I would guess that the numbers would be more uniformly distributed.

Second, since time advances, depending on the CPU speed, there can be sequences of increasing random numbers. That isn't desirable in a true random number generator. It appears that adding the factorial is a kludge to force the numbers to exceed 1000 and thus get truncated.

I see no logical reason to use a string to represent your strdate variable, nor why you are limited to generating only 20 values at a time. Why not get a number when requested, or generate an arbitrary number? I have done a fair amount of stochastic modeling, but I have to admit I have never yet needed a random number generator for integers between 1 and 1000, but I have often needed floating point generators between 0 and 1 which can then be scaled to most any range desired.

If you are trying to generate numbers without duplicates, there are methods to do precisely that. They are commonly used in card games, etc. You offer no comparisons nor explain why your algorithm should be used rather than others.

While your code might be useful in some applications, I think you need to revisit the theory behind random number generators and sequences.

modified on Wednesday, June 1, 2011 10:29 PM

GeneralRe: My vote of 2 Pin
Haitham Alany2-Jun-11 3:12
Haitham Alany2-Jun-11 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.