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

  • SEARCH
  • Home
  • 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 8242657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:14:37+00:00 2026-06-07T21:14:37+00:00

I was trying to implement an extension of Random class. But this function will

  • 0

I was trying to implement an extension of Random class.

But this function will have one feature, you can get a number followed step. For example:

RandNumb(double min, double max, double step)
RandNumb(1, 10, 2) = 6
RandNumb(100, 1000, 500) = 1000
RandNumb(0.001, 0.1, 0.01) = 0.15

My first idea was to get a random number from min to max, and compare if this is a valid number (because of the step). If not, again generate another number.

But I’m sure that this is not a good performance. What do you think?

  • 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-07T21:14:38+00:00Added an answer on June 7, 2026 at 9:14 pm

    By definition of the problem, it seems that the valid values ​​for “RandNumb (1, 10, 2)” would be: 1, 3, 5, 7, 9.

    In that case. The code would look like:

    class Program
    {
        static void Main(string[] args)
        {
            Random rd = new Random();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("RandNumb(rd, 1, 10, 2)=" + RandNumb(rd, 1, 10, 2));
                Console.WriteLine("RandNumb(rd, 100, 1000, 500)=" + RandNumb(rd, 100, 1000, 500));
                Console.WriteLine("RandNumb(rd, 0.001, 0.1, 0.01) =" + RandNumb(rd, 0.001, 0.1, 0.01)); 
            }
            Console.ReadLine();
        }
    
        public static double RandNumb(Random rd, double min, double max, double step)
        {
            int range = (int)Math.Floor((max - min) / step);
            int stepCount = rd.Next(0, range + 1);
            return min + (step * stepCount);
        }
    }
    

    And the result would look like this:

    RandNumb(rd, 1, 10, 2)=3
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,071
    RandNumb(rd, 1, 10, 2)=5
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,041
    RandNumb(rd, 1, 10, 2)=3
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,081
    RandNumb(rd, 1, 10, 2)=9
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,011
    RandNumb(rd, 1, 10, 2)=7
    RandNumb(rd, 100, 1000, 500)=100
    RandNumb(rd, 0.001, 0.1, 0.01) =0,091
    RandNumb(rd, 1, 10, 2)=5
    RandNumb(rd, 100, 1000, 500)=100
    RandNumb(rd, 0.001, 0.1, 0.01) =0,001
    RandNumb(rd, 1, 10, 2)=1
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,071
    RandNumb(rd, 1, 10, 2)=1
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,061
    RandNumb(rd, 1, 10, 2)=5
    RandNumb(rd, 100, 1000, 500)=100
    RandNumb(rd, 0.001, 0.1, 0.01) =0,041
    RandNumb(rd, 1, 10, 2)=3
    RandNumb(rd, 100, 1000, 500)=600
    RandNumb(rd, 0.001, 0.1, 0.01) =0,021
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a LINQ-to-SQL extension method that will simply get a random
I'm trying to implement an XSLT extension function with variable number of arguments. If
I am received the following error while trying to implement a C# extension function
I am trying to implement an extension method that will work with linq2entities. I
Trying to implement a search similar to here .This searches properties based on city,locality,property
I am trying to create a simple 3-D app for android that will have
I'm trying to implement this basic example found here : under the section titled
I am trying to implement a function in C (Extending Python) to return a
Ok so this might be a dumb question, but I can't seem to figure
I am trying to write an extension that can access the TextUndoHistory of an

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.