Results 1 to 2 of 2

Thread: ***RESOLVED***math.random odd integers

  1. #1

    Thread Starter
    Addicted Member annie613's Avatar
    Join Date
    May 2003
    Location
    NY, NY
    Posts
    161

    ***RESOLVED***math.random odd integers

    is there a way to use Math.random and only request odd integers??? this is the code i use for n as any random number...

    Code:
    n = (int)Math.floor(Math.random()*50+1);

    but i want n to be odd each time...
    any suggestions
    thanks in advance annie
    Last edited by annie613; Aug 2nd, 2004 at 02:24 PM.

  2. #2

    Thread Starter
    Addicted Member annie613's Avatar
    Join Date
    May 2003
    Location
    NY, NY
    Posts
    161
    i think i did it
    after suffering by putting this as a while loop
    and
    while (n !=0)
    if(n % 2 == 0 ) //if even choose another num
    else //odd was produces; contine with code

    i just did this
    Code:
    n = (int)Math.floor(Math.random()*50+1);
    Random r = new Random();
    n = ( 2 * r.nextInt(25) ) +1;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width