| |

VerySource

 Forgot password?
 Register
Search
View: 800|Reply: 8

The problem of random numbers

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-4 16:50:01
| Show all posts |Read mode
Regarding generating a random number from -500 to 500, I thought about it for a long time and only came up with a -500 to 0. After generating a 0 to 500, adding them up was a random number between -500 and 500. `` `But I'm sure there are other ways or my method is wrong```Who knows '' please
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-4 17:51:01
| Show all posts
java.lang.Math.random ()
It produces 0 to 1 double-precision numbers. Subtract 0.5 from the random number generated before multiplying by 1000. See if it helps you.
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 21:42:01
| Show all posts
Math.random () * 1000) -500;
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 21:45:01
| Show all posts
(int) (Math.random () * 1000) -500
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 02:45:01
| Show all posts
I love you guys! `` Thank you brother on the second floor
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-15 17:36:01
| Show all posts
Oh, communicate a lot.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-22 10:45:01
| Show all posts
In JDK5.0 there is a class import java.util.Random. *; Where the method nextInt can help you complete
First declare Random Random = new Random ();
int a = 0-random.nextInt (501)
public int nextInt (int n) returns a pseudo-random number, an int value uniformly distributed between 0 (inclusive) and the specified value (exclusive), taken from the sequence of this random number generator. The general contract of nextInt is to generate and return an int in the specified range pseudo-randomly. All n possible int values ​​are generated with (roughly) the same probability. The Random class implements the nextInt (int n) method as follows:

 public int nextInt (int n) {
     if (n <= 0)
                throw new IllegalArgumentException ("n must be positive");

     if ((n&-n) == n) // i.e., n is a power of 2
         return (int) ((n * (long) next (31)) >> 31);

     int bits, val;
     do {
         bits = next (31);
         val = bits% n;
     } while (bits-val + (n-1) <0);
     return val;
 }
 The ambiguous word "rough" was used in the previous description, just because the next method is roughly a fair source of individually selected bits. If it is the best source for randomly selecting bits, then the given algorithm should choose int values ​​completely consistently from the starting range.

But this algorithm is slightly more complicated. It rejects values ​​that cause an uneven distribution (because 2 ^ 31 cannot be divisible by n). The probability of a value being rejected depends on n. The worst case is n = 2 ^ 30 + 1, the probability of rejection is 1/2, and the estimated number of iterations before the loop terminates is 2.

This algorithm specifically treats cases where n is a power of two: it returns the correct high number of bits from the underlying pseudo-random number generator. In the case of no special handling, the correct low digits are returned. It is well known that linear congruence pseudorandom number generators (such as this class implements) have short periods in their low-value sequence of values. Therefore, if n is a power of two (lower power), this special case causes subsequent calls to this method to return a sequence of values ​​whose length is greatly increased.


parameter:
n-the range of random numbers returned. Must be positive.
return:
A pseudo-random number with an evenly distributed int value between 0 (inclusive) and n (inclusive).
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-23 09:36:01
| Show all posts
^
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-23 09:54:01
| Show all posts
Math.random () * 1000) -500;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list