Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:51:30+00:00 2026-06-12T01:51:30+00:00

How to generate an odd Random number between a given range.. For Eg: For

  • 0

How to generate an odd Random number between a given range..

For Eg: For range between 1 to 6 ..
Random No is 3 or 1 or 5

Method for Generating Random No :

    Random_No = Min + (int)(Math.Random()*((Max-Min)+1))

Refer How do I generate random integers within a specific range in Java?

Method For Generating Odd Random No :

    Random_No = Min + (int)(Math.Random()*((Max-Min)+1))
    if(Random_No%2 ==0)
    {
          if((Max%2)==0)&&Random_No==Max)
          {
              Random_No = Random_No - 1;  
          }
          else{
              Random_No = Random_No +1;
          }
    }

This Function will always convert 2 into 3 and not 1
Can we make this a more random function which can convert 2 sometimes into 3 and sometimes into 1 ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:51:31+00:00Added an answer on June 12, 2026 at 1:51 am

    Assuming max is inclusive, I’d suggest the following:

    if (Max % 2 == 0) --Max;
    if (Min % 2 == 0) ++Min;
    Random_No = Min + 2*(int)(Math.random()*((Max-Min)/2+1));
    

    It results in even distribution among all the odd numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any good method to generate random even numbers with C#? I came
What I know: Rails has the cycle() method that enables odd/even rows in a
I have a simple question and I'm a little rusty on random number generation.
I need two function.one will generate random even numbers and 2nd will generate random
I have come across a rather odd situation. I have generated a a random
I've been trying to find a more pythonic way of generating random string in
Possible Duplicate: php: how to add odd/even loop in array I am generating a
I'm getting odd behavior (it generates only missing values) from the following loop -
To generate the following markup: <label class=foo>Bar</label> The PHP will look something like: <?php
I generate EMMA reports during my build, they look like this <report> <stats> <packages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.