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