There is a number 0,000099999999999999999999999999 . How to generate a range of numbers from this number to 0,999999999999999999999999999999 . 0,999999999999999999999999999999 result, it should turn out

 0,000099999999999999999999999999 0,000100000000000000000000000000 0,000100000000000000000000000001 ... 0,999999999999999999999999999999 
  • In your description, the starting and ending values ​​of the range are the same. - Vlad from Moscow
  • sorry Corrected - Mehelta
  • 2
    Standard numeric types in C # do not have enough accuracy (in decimal places) of expressing certain numbers from a sequence. what type of sequence elements do you expect? double? - PashaPash
  • 7
    By the way, does it bother you that you need to generate almost 10 ^ 30 numbers? You can not even save them anywhere .. Where do you have so much and why? - Kromster
  • 2
    can describe the task you are solving, something tells me that you are not doing something. During the complete enumeration of this range, it will have time to be born, grow old and disappear a dozen of other universes, successively of course =) - rdorn

2 answers 2

 namespace BigDec { using Deveel.Math; public class Program { public static void Main() { MathContext mc = new MathContext(26); BigDecimal delta=new BigDecimal(1, 25, mc); BigDecimal start = new BigDecimal(1, 4, mc).Subtract(delta); for (; start != 1; start=start+delta) { Console.WriteLine(start.ToPlainString()); } } } } 

Pre-add from nuGet dmath

  • one
    for 1 million years the program will be executed? - 4per
  • @ 4per ~ 10 ^ 22 years, it was more accurate to count laziness, well, maybe I was mistaken by 3-5 orders of magnitude, I did not take into account the speed of calculations by the machine, but this does not do the weather. - rdorn
  • one
    @ 4per, sometimes the process itself is important, but not the result;) - Qutrix
  • 2
    Reminded about GUIDs . - BlackWitcher
 function randomInteger(min, max) { var rand = min + Math.random() * (max - min) rand = Math.round(rand); return rand; } 

This feature works. But at the same time it is incorrect: the probability to get the extreme values ​​of min and max will be two times less than any other.

When you run this code multiple times, you will easily notice that 2 drops out most often.

This is due to the fact that Math.round () receives a variety of random numbers from 1 to 3, but rounding to the nearest integer will result in:

values ​​from the range 1 ... 1.49999 .. become 1 values ​​from the range 1.5 ... 2.49999 .. become 2 values ​​from the range 2.5 ... 2.99999 .. become 3 From here it is clearly seen that the range falls in 1 (as well as 3) values ​​are two times smaller than in 2. Because of this, such a bias.

  • I meant not just randomly generating, but consistently - Mehelta
  • @ S.Kost topicaster need all the numbers from the range. it's like to get a sequence of 1, 2, ... 10 to generate random 1000 times, and then sort it - it will almost always work. - PashaPash