I
I
Ivan Antonov2015-05-01 22:24:25
Programming
Ivan Antonov, 2015-05-01 22:24:25

How to write an equation?

You need to convert a random number from 0 to 1 to a number from 25 to 325 and this number must be a multiple of 25.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2015-05-01
@antonowano

325/25=13;
round((rand()*13))*25;
round(rand()*13) will give integers from 0 to 13.
Multiplying an integer by 25 will give a multiple of 25.
25 is added at the end to get the offset.

M
Maxim Kudryavtsev, 2015-05-01
@kumaxim

Everything is elementary:

  1. We generate a random number from 0 to 10. Divide it by 10 (let's call it N1)
  2. We generate a random number from 25 to 325 (let's call it R1)
  3. We add N1 + R1. Round up to a whole value (up or down, see for yourself)
  4. We calculate the remainder of dividing N1 + R1 by 25. In most languages, this operation goes through "%"
  5. If the remainder of the division is zero - the result is found, exit
  6. If the remainder of the division is not zero, repeat steps 2-4. About the cycle, I think, it is not necessary to talk

D
Deerenaros, 2015-05-02
@Deerenaros

print random.choice([i for i in xrange(25, 326, 25)])

I mean, in addition to casting (0, 1] to the required interval, you can use simpler methods. Moreover, two randoms are not ice if they are honest, that is, there are few of them or they are slow. In your programming language (or rather, in your library) is likely to have something similar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question