I made a randomizer, but I see that a mistake in the formula gives out incorrect numbers.

int a = 0; int oti = Integer.parseInt(ots); int doi = Integer.parseInt(dos); a = (int) (a - Math.random() * (doi - oti)); otvet.setText(String.valueOf(a)); 

I want random numbers to be generated between oti and doi . Better to have both borders

  • I read it just like that, I can also give the full code, now - F0x1db
  • I understand, but you asked about oti and doi, I gave you the full code - F0x1db
  • ot - the number from which random home numbers should begin, do - to which. I want random numbers to be generated between ot and do. ots and so on. - string, oti - int. - F0x1db
  • and you need a negative number? - Komdosh 5:48 pm
  • Yes, it is better to include - F0x1db

1 answer 1

The random number from oti to doi , including both boundaries:

 int a = (int)(Math.random() * (doi - oti + 1)) + oti; 

The random number from oti to doi , including only the border oti :

 int a = (int)(Math.random() * (doi - oti)) + oti; 
  • excellent, thank you very much - F0x1db pm
  • alternative int a = (int) Math.floor ((Math.random () * doi) + oti); - SupinePandora43 5:53 pm
  • @ SupinePandora43 no, random * doi + oti will not work correctly and produce results from oti to oti + doi . - Regent
  • I have this code with int works fine. and enters my 'library' (just grade 1 with a ton of code) for android - SupinePandora43
  • @ SupinePandora43 is an example of the fact that your version does not work correctly. Are you sure about "excellent"? - Regent