Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   random numbers in java? (http://forum.vcoderz.com/showthread.php?t=21194)

mr_j 12-16-2011 08:29 PM

random numbers in java?
 
I need to generate random numbers in java, and I need them to be completely evenly distributed(or close to that) specifically I need to generate around 300 numbers between 1 and 100...

math random method is not helping, nor any other traditional random method. the problem is the numbers I get from those methods repeat themselves after a certain time, or that they are not evenly distributed(so I get more than half the numbers below 50 and less than half above it...)

any ideas?

Google 12-18-2011 09:23 PM

Mathematicaly, that's a random number generator:
x 0 = given, x n+1 = P 1 x n + P 2 (mod N) n = 0,1,2,...
The parameters P 1, P 2, and N determine the characteristics of the random number generator, and the choice of x 0 (the seed ) determines the particular sequence of random numbers that is generated. In your case N is 101.

2eza ma beddak twajje3 rasak, try this:
Quote:

import java.util.Random;
class rndGen
{
static Random wheel = new Random();

public static void main(String[] args)
{
for(int i=0; i<300; i++)
{
// generate an int in range 0..100 inclusive.
int low = 0;
int high = 100;
int m = wheel.nextInt( high - low + 1 ) + low;
System.out.print( m + " ");
}
}
}

mr_j 12-18-2011 10:32 PM

thanks, I actually was eventually able to do my code without the need to change the math random thingy, I'll keep your post in mind for future reference though


All times are GMT +1. The time now is 02:57 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger