1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
| /*
* Created on 17 mai 2005
* you can use it as ever you want
*/
package random;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Random;
import javax.swing.JTextField;
/**
* @author komando
* @version 1.0
* Class RandomObject uses java.util.Random to generate various random
* such as Date, Time, Long, Int
*/
public class RandomObject extends Random{
private static final char chars[]={'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private static final int LIMIT = 1000;
private long echelle;
private int init;
private static final double D_LIMIT = 1.0;
private double dechelle;
private double dinit=0.0;
public RandomObject(){
super();
}
/**
* @param debut
* @param fin
* @return this int between debut and fin
*/
public int nextInt(int debut, int fin) {
int val = this.nextInt(fin);
return debut + val * (fin - debut) / fin;
}
/**
* converts String to int
* @param debutstr
* @param finstr
* @return this int between debut and fin
*/
public int nextInt(String debutstr, String finstr) {
int debut, fin;
try{
debut = Integer.parseInt(debutstr);
fin = Integer.parseInt(finstr);
}
catch(NumberFormatException e){
e.getMessage();
return -1;
}
return nextInt(debut,fin);
}
/**
* reads txtFields and return this int between them
* @param debtxtField
* @param fintxtField
* @return this int between txtFields
*/
public int nextInt(JTextField debtxtField,JTextField fintxtField) {
return nextInt(debtxtField.getText(),fintxtField.getText());
}
/**
* reads txtFields and return this int between them
* @deprecated
* @param txtFields
* @return andom int between txtFields
*/
public int nextInt(JTextField txtFields[]) {
return nextInt(txtFields[0].getText(),txtFields[1].getText());
}
/**
* @param debut
* @param fin
* @return this long between long debut and fin
*/
public long nextLong(long debut,long fin){
int randint;
int adjust;
long randlong;
//setting a scale an generating this int
echelle = fin / LIMIT;
init = (int) (debut / echelle);
randint = this.nextInt(LIMIT);
//adjusting this int and resetting the scale
adjust = init + randint * (LIMIT - init) / LIMIT;
randlong = adjust * echelle;
return randlong;
}
/**
*
* @param debut
* @param fin
* @return this long between String debut and fin
*/
public long nextLong(String debutstr,String finstr){
long debut,fin;
try{
debut=(long) Integer.parseInt(debutstr);
fin=(long) Integer.parseInt(finstr);
}
catch(NumberFormatException e){
e.getMessage();
return -1;
}
return nextLong(debut,fin);
}
/**
*
* @param debut
* @param fin
* @return this date between long datedebut and datefin
*/
public Date nextDate(long debut, long fin){
return new Date(nextLong(debut,fin));
}
/**
* @param datedebut
* @param datefin
* @return this date between Date datedebut and datefin
*/
public Date nextDate(Date datedebut, Date datefin) {
long debut, fin;
//return dates in ms
debut = datedebut.getTime();
fin = datefin.getTime();
return nextDate(debut,fin);
}
/**
* @param debut
* @param fin
* @return this date between string debut and fin
*/
public Date nextDate(String debutstr, String finstr) {
Date datedebut, datefin;
datedebut = Date.valueOf(debutstr);
datefin = Date.valueOf(finstr);
return nextDate(datedebut,datefin);
}
/**
*
* @param debtxtField
* @param fintxtField
* @return this date between Fields debut and fin
*/
public Date nextDate(JTextField debtxtField,JTextField fintxtField) {
return nextDate(debtxtField.getText(),fintxtField.getText());
}
/**
* @deprecated
* @param txtFields
* @return this date between Fields txtFields[0] and txtFields[1]
*/
public Date nextDate(JTextField txtFields[]){
return nextDate(txtFields[0].getText(),txtFields[1].getText());
}
public Time nextTime(long debut,long fin){
return new Time(nextLong(debut,fin));
}
/**
* @param timedebut
* @param timefin
* @return this Time between long debut and fin
*/
public Time nextTime(Time timedebut,Time timefin){
long debut, fin;
//return dates in ms
debut = timedebut.getTime();
fin = timefin.getTime();
return nextTime(debut,fin);
}
/**
* @param debut
* @param fin
* @return this Time between string debut and fin
*/
public Time nextTime(String debutstr, String finstr) {
Time timedebut, timefin;
timedebut = Time.valueOf(debutstr);
timefin = Time.valueOf(finstr);
return nextTime(timedebut,timefin);
}
/**
*
* @param debtxtField
* @param fintxtField
* @return this Time between Fields debut and fin
*/
public Time nextTime(JTextField debtxtField,JTextField fintxtField) {
return nextTime(debtxtField.getText(),fintxtField.getText());
}
/**
* @deprecated
* @param txtFields
* @return this Time between Fields txtFields[0] and txtFields[1]
*/
public Time nextTime(JTextField txtFields[]){
return nextTime(txtFields[0].getText(),txtFields[1].getText());
}
/**
*
* @param debut
* @param fin
* @return
*/
public Timestamp nextTimestamp(long debut,long fin){
return new Timestamp(nextLong(debut,fin));
}
/**
* @param timedebut
* @param timefin
* @return
*/
public Timestamp nextTimestamp(Timestamp timedebut,Timestamp timefin){
long debut, fin;
//return dates in ms
debut = timedebut.getTime();
fin = timefin.getTime();
return nextTimestamp(debut,fin);
}
/**
* @param debut
* @param fin
* @return this Timestamp between string debut and fin
*/
public Timestamp nextTimestamp(String debutstr, String finstr) {
Timestamp timedebut, timefin;
timedebut = Timestamp.valueOf(debutstr);
timefin = Timestamp.valueOf(finstr);
return nextTimestamp(timedebut,timefin);
}
/**
*
* @param debtxtField
* @param fintxtField
* @return this Timestamp between Fields debut and fin
*/
public Timestamp nextTimestamp(JTextField debtxtField,JTextField fintxtField) {
return nextTimestamp(debtxtField.getText(),fintxtField.getText());
}
/**
* @deprecated
* @param txtFields
* @return this Timestamp between Fields txtFields[0] and txtFields[1]
*/
public Timestamp nextTimestamp(JTextField txtFields[]){
return nextTimestamp(txtFields[0].getText(),txtFields[1].getText());
}
/**
*
* @return
*/
public char nextChar(){
return chars[nextInt(chars.length)];
}
/**
* @deprecated
* @return
*/
public String nextString(){
String str="";
for (int i = 0; i < nextInt(); i++) {
str+=nextChar();
}
return str;
}
public String nextString(int lenght){
String str="";
for (int i = 0; i < lenght; i++) {
str+=nextChar();
}
return str;
}
/**
*
* @param debut
* @param fin
* @return
*/
public float nextFloat(float debut,float fin){
float randint;
float adjust;
float randlong;
//setting a scale an generating this float
dechelle = fin / D_LIMIT;
dinit = debut / dechelle;
randint = this.nextFloat();
//adjusting this int and resetting the scale
adjust = (float) (dinit + randint * (D_LIMIT - dinit) / D_LIMIT);
randlong = (float) (adjust * dechelle);
return randlong;
}
/**
*
* @param strdebut
* @param strfin
* @return
*/
public float nextFloat(String strdebut, String strfin){
float debut,fin;
try{
debut = Float.parseFloat(strdebut);
fin = Float.parseFloat(strfin);
}
catch(NumberFormatException e){
e.getMessage();
return -1;
}
return nextFloat(debut,fin);
}
/**
*
* @param debTextfield
* @param finTextfield
* @return
*/
public float nextFloat(JTextField debTextfield,JTextField finTextfield){
return nextFloat(debTextfield.getText(),finTextfield.getText());
}
/**
* @deprecated
* @param txtFields
* @return
*/
public float nextFloat(JTextField txtFields[]){
return nextFloat(txtFields[0],txtFields[1]);
}
/**
*
* @param debut
* @param fin
* @return
*/
public double nextDouble(double debut,double fin){
double randint;
double adjust;
double randlong;
//setting a scale an generating this double
dechelle = fin / D_LIMIT;
dinit = debut / dechelle;
randint = this.nextDouble();
//adjusting this int and resetting the scale
adjust = (dinit + randint * (D_LIMIT - dinit) / D_LIMIT);
randlong = (adjust * dechelle);
return randlong;
}
/**
*
* @param strdebut
* @param strfin
* @return
*/
public double nextDouble(String strdebut, String strfin){
double debut,fin;
try{
debut = Double.parseDouble(strdebut);
fin = Double.parseDouble(strfin);
}
catch(NumberFormatException e){
e.getMessage();
return -1;
}
return nextDouble(debut,fin);
}
/**
*
* @param debTextfield
* @param finTextfield
* @return
*/
public double nextDouble(JTextField debTextfield,JTextField finTextfield){
return nextDouble(debTextfield.getText(),finTextfield.getText());
}
/**
* @deprecated
* @param txtFields
* @return
*/
public double nextDouble(JTextField txtFields[]){
return nextDouble(txtFields[0],txtFields[1]);
}
/**
* Test Method
* @param args
*/
public static void main(String args[]){
RandomObject r=new RandomObject();
for (int i = 0; i < 1000; i++) {
// System.out.println("random int "+r.nextInt(100,1000));
// System.out.println("random int "+r.nextInt("100","1000"));
// System.out.println("random long "+r.nextLong(1000000L,10000000L));
// System.out.println("random date "+r.nextDate("2005-01-01","2005-12-31"));
// System.out.println("random time "+r.nextTime("08:00:00","23:00:00"));
// System.out.println(r.nextString(5));
System.out.println(r.nextDouble(2.20,58.215));
}
}
} |
Partager