Results 1 to 4 of 4

Thread: Problem in random number

  1. #1
    Join Date
    Nov 2009
    Posts
    446

    Problem in random number

    Hi
    I am new to java and I am trying a program to random numbers. What i am trying is to create 50 random numbers which are assigned to different vales. I have tried this by using a do while loop for this. Please check the following code.
    Code:
    int gennum = 0;
    int curval = 2;
                       
    do {
          int rannum = (int)Math.round((Math.random() * 365) + 1); 
          }
    
    while (gennum < 49) {
                           
           int rannum.currentValue = (int)Math.round((Math.random() * 365) + 1);
    
                        currentValue++;
                        generagteNum++;
           }
    When I compile it I get any error. Any advice on this or help.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem in random number

    Hello
    I think you are getting confused by the code. I think the way or the direction in which you are thinking for the program is wrong.
    do {
    int rannum = (int)Math.round((Math.random() * 365) + 1);
    }

    while (gennum < 49) {
    I do not think you have to use do while loop in this program, there is not need at all. I would recommend you to use array in this situation. Try array and then post your code. Surely array will be the good choice for doing this.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Problem in random number

    Hi
    I am sorry, I forget to think about the array. I think using array I can do this quite easily. By the way thank for the tip. But I think my first code was a thinking code for interested people. I am new to this language and any help to improve my code will be highly appreciated. Thanks in advance.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem in random number

    Hi
    I think this code might help you
    Code:
    final int[] integers = new int[50]; // create the array to hold 50 primitive integers
    final ran ran = new ran(); // create the ran generator, can be found in java.util package
    
    // Loop 50 times and create a ran number each time and assign it to the correct index in the array
    for(int index = 0; index < 50; ++index) {
        integers[index] = ran.nextInt(); // generate number and store in the array
    }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Problem in random number subtraction
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 07:37 PM
  3. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  4. Random number generation in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 11:42 AM
  5. Generate random number using VB
    By Bambina in forum Software Development
    Replies: 2
    Last Post: 02-09-2009, 10:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,712,100.93595 seconds with 16 queries