Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies « Everything related to computers and internet. »

Reply
 
Share Thread Tools Search this Thread
Old 12-16-2011   #1
mr_j
Community Moderator
 
mr_j's Avatar
 
Last Online: 07-31-2023
Join Date: Oct 2007
Posts: 2,520
Thanks: 3,023
Thanked 2,076 Times in 1,156 Posts
Groans: 7
Groaned at 34 Times in 27 Posts
Default 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?


__________________
"Loyalty to the country always. Loyalty to the government when it deserves it. "
-Mark Twain

Last edited by mr_j; 12-16-2011 at 08:40 PM.
mr_j is offline   Reply With Quote
Old 12-18-2011   #2
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

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 + " ");
}
}
}
__________________

Google is offline   Reply With Quote
Old 12-18-2011   #3
mr_j
Community Moderator
 
mr_j's Avatar
 
Last Online: 07-31-2023
Join Date: Oct 2007
Posts: 2,520
Thanks: 3,023
Thanked 2,076 Times in 1,156 Posts
Groans: 7
Groaned at 34 Times in 27 Posts
Default

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
__________________
"Loyalty to the country always. Loyalty to the government when it deserves it. "
-Mark Twain
mr_j is offline   Reply With Quote
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
java, numbers, random



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:27 AM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share