Link to home
Create AccountLog in
Avatar of waltbaby315
waltbaby315

asked on

How do you get this program to generate different numbers out of the output

How do you get this program to  generate different numbers out of the output ?

public class First
{
public static void main(String[] args) {
int n = (int)Math.floor(Math.random()*100000+1);
int digit;
System.out.println(" Number generated 235:235 ");
System.out.println(" Sum of digits =26 ");
}
}
Avatar of CompProbSolv
CompProbSolv
Flag of United States of America image

I'm afraid that I don't know anything about AS400 programming, but do recall a Randomize () command in some other language (BASIC?).  It would set a new "seed" in the randomizer so you'd get a different set of numbers.  The seed was put in the parentheses.  If you used the system timer to provide the seed for that function, you could have a pretty good approximation of randomness.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Your class always generates different numbers
I ran several times and they do not repeat
at least inmy environement:

public class First
{
public static void main(String[] args) {
    for(int j=0; j<10; j++){
int n = (int)Math.floor(Math.random()*100000+1);
    System.out.println(n);
    }
int digit;
System.out.println(" Number generated 235:235 ");
System.out.println(" Sum of digits =26 ");
}
}

Open in new window


First run:

51323
21948
83732
56684
50138
67735
86410
81695
14470
1583

Open in new window


Second run:
44085
53945
46097
87346
96627
61915
53862
70963
50484
6035

Open in new window


Third run:
29810
34882
49513
33293
52201
20012
64157
53449
31641
74458

Open in new window


//for example to print 100 numbers

public class First
{

public static void main(String a[])
{

          Random random = new Random():
           while(count < 100)
           {
                      System.out.println(random.nextInt());
                      count++;
           }
}

}