13,146,014 members (42,662 online)
alternative version

Stats

14.4K views
6 bookmarked
Posted 1 Jun 2011

Random Ranged Number Generator

, 1 Jun 2011
 Rate this:
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.

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

Share

 Software Developer Optimiza Iraq
No Biography provided

You may also be interested in...

 First Prev Next
 two-digit numbers Amir Garajedaghi19-Oct-16 8:53 Amir Garajedaghi 19-Oct-16 8:53
 My vote of 3 skv_lviv6-Jun-11 18:33 skv_lviv 6-Jun-11 18:33
 Re: My vote of 3 Haitham Alany6-Jun-11 21:09 Haitham Alany 6-Jun-11 21:09
 [My vote of 2] Reinventing the wheel Dave Brauns3-Jun-11 6:49 Dave Brauns 3-Jun-11 6:49
 Re: [My vote of 2] Reinventing the wheel Haitham Alany4-Jun-11 23:31 Haitham Alany 4-Jun-11 23:31
 Re: [My vote of 2] Reinventing the wheel hakkietakkie17-Jul-11 23:57 hakkietakkie 17-Jul-11 23:57
 My vote of 2 Phillip4652-Jun-11 6:38 Phillip465 2-Jun-11 6:38
 My vote of 2 Walt Fair, Jr.1-Jun-11 16:20 Walt Fair, Jr. 1-Jun-11 16:20
 Re: My vote of 2 Haitham Alany2-Jun-11 3:12 Haitham Alany 2-Jun-11 3:12
 At First I would like to thank you for replying and spotting the lights on some important issues in my article. Now allow me to answer you and respond on the points you mentioned above: Your First Point ---------------- I stated that the generated numbers are distributed because of the result it produced by giving a vary randomized numbers. Your Second Point ----------------- When I used only the time (without Factorial) I got a sequenced result that may be predicted however, using the factorial function added a randomness to my function and make it difficult to be predicted and that's why I don't agree with you about your point which is the factorial solution is a "kludge" Your Third Point ---------------- You are right as I don't need to use the strdate but I don't agree with you about the range random number as sometimes you need a randomness within a known range. My conclusion ------------- My algorithm is not the best and will not be the best but at least it gives a different idea about the randomness and it uses different techniques as well ,who knows may someone does some improvements on it and makes it working better, Thank you again
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Sep-17 23:53 Refresh 1