So, I was doing to experimenting, looking for ways to generate random floats.
I stumbled upon this link:
Best way to generate a random float in C#
So, I tried it out and surely enough it generated floats alright but they were very small and very large. For my purposes I really just wanted one integer with two decimal places and using that method would require even more resources to shorten.
So I decided mess around and out of all the ways I tried, to my shock, the fastest time came from literally generating random strings such as 1.25 and parsing it. I suspect this is because the switch is converted to a hash table but I'm not quite sure.
So, here's the code.
Please Input Iteration Count:
30
(0) Float Generated: [7.4]
(1) Float Generated: [6.13]
(2) Float Generated: [4.33]
(3) Float Generated: [6.86]
(4) Float Generated: [3.63]
(5) Float Generated: [8.42]
(6) Float Generated: [3.18]
(7) Float Generated: [1.6]
(8) Float Generated: [3.21]
(9) Float Generated: [5.41]
(10) Float Generated: [2.53]
(11) Float Generated: [2.66]
(12) Float Generated: [0.62]
(13) Float Generated: [2.1]
(14) Float Generated: [1.62]
(15) Float Generated: [5.44]
(16) Float Generated: [6.45]
(17) Float Generated: [7.13]
(18) Float Generated: [5.67]
(19) Float Generated: [0.38]
(20) Float Generated: [2.78]
(21) Float Generated: [1.33]
(22) Float Generated: [3.1]
(23) Float Generated: [6.77]
(24) Float Generated: [8]
(25) Float Generated: [7.88]
(26) Float Generated: [7.77]
(27) Float Generated: [6.88]
(28) Float Generated: [4.33]
(29) Float Generated: [0.5]
Time To Generate (ms): 10
(0) Float Generated: [-2.826086E-36]
(1) Float Generated: [-2.973886E+30]
(2) Float Generated: [-6.708448E+10]
(3) Float Generated: [-3.570353E+17]
(4) Float Generated: [2.977412E-32]
(5) Float Generated: [1.40951E+36]
(6) Float Generated: [7.259578E+22]
(7) Float Generated: [-2.021946E+18]
(8) Float Generated: [2.332486E+08]
(9) Float Generated: [-1.158906E+19]
(10) Float Generated: [1.426E-15]
(11) Float Generated: [-1.830809]
(12) Float Generated: [-4.029474E+31]
(13) Float Generated: [-1.407781E+27]
(14) Float Generated: [3.576921E+37]
(15) Float Generated: [5.448703E+10]
(16) Float Generated: [0.09467465]
(17) Float Generated: [5.176487E+19]
(18) Float Generated: [2.789317E-05]
(19) Float Generated: [-2.793652E+17]
(20) Float Generated: [-2.07439E-07]
(21) Float Generated: [6.464674]
(22) Float Generated: [1.120899E-22]
(23) Float Generated: [1.294979E-24]
(24) Float Generated: [-6.040918E+34]
(25) Float Generated: [-0.0001687571]
(26) Float Generated: [-1.558992E-38]
(27) Float Generated: [-3.822325E-16]
(28) Float Generated: [-7.072877E+36]
(29) Float Generated: [-7.021242E-20]
Time To Generate (ms): 36
Please Input Iteration Count:
Interestingly enough - this time difference appears to change. Some time they're neck and neck and other times what you see above occurs, where the strings vastly out preform.
So, I have three questions:
1.) Why would this time discrepancy occur?
2.) Why can't I generate floats with .Net's Random class like I can integers?
3.) How many other ways can I create random floats efficiently? I know there's literally infinite ways but I'm referring to the realistic methods that could be used in consumer applications.
Aucun commentaire:
Enregistrer un commentaire