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 8976417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:07:42+00:00 2026-06-15T19:07:42+00:00

I am writing a random-value generator in C#, this generic function should return value

  • 0

I am writing a random-value generator in C#, this generic function should return value of Type Bool, Int64, Int32, Double depending on the Type passed. So, I can pass the System.Type as a method parameter, but how should I define the return type?

For example

GetRandomValueByType(TypeCode.Boolean) <— Returns Boolean
GetRandomValueByType(TypeCode.Double) <— Returns Double
GetRandomValueByType(TypeCode.Int32) <— Returns Int32

And so on and so forth.

Thank you!

—————————EDIT——————————–

This is the code I used:

if (ta.IsPrimitive || Type.GetTypeCode(ta) == TypeCode.String)
{
    Random rnd = new Random();
    var buffer = new byte[sizeof(Int64)];
    rnd.NextBytes(buffer);
    switch (Type.GetTypeCode(ta))
    {
        case TypeCode.Boolean:
            oArr[ctr] = (rnd.Next(100) % 2 == 0);
            break;
        case TypeCode.Byte:
            oArr[ctr] = buffer[0];
            break;
        case TypeCode.SByte:
            oArr[ctr] = (sbyte)buffer[0];
            break;
        case TypeCode.Char:
            oArr[ctr] = Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65));
            break;
        case TypeCode.Decimal:
            oArr[ctr] = NextDecimal(rnd);
            break;
        case TypeCode.Double:
            oArr[ctr] = rnd.NextDouble() * rnd.Next(Int32.MaxValue);
            break;
        case TypeCode.Single:
            var buf = new byte[sizeof(Single)];
            rnd.NextBytes(buf);
            oArr[ctr] = BitConverter.ToSingle(buffer, 0);
            break;
        case TypeCode.Int32:
            oArr[ctr] = rnd.Next(Int32.MinValue, Int32.MaxValue);
            break;
        case TypeCode.UInt32:
            oArr[ctr] = rnd.Next(Int32.MaxValue) + (rnd.Next(100) % 2) * rnd.Next(Int32.MaxValue);
            break;
        case TypeCode.Int64:
            oArr[ctr] = BitConverter.ToInt64(buffer, 0);
            break;
        case TypeCode.UInt64:
            oArr[ctr] = BitConverter.ToUInt64(buffer, 0);
            break;
        case TypeCode.Int16:
            oArr[ctr] = rnd.Next(Int16.MaxValue);
            break;
        case TypeCode.UInt16:
            oArr[ctr] = rnd.Next(Int16.MaxValue) + (rnd.Next(100) % 2) * rnd.Next(Int16.MaxValue);
            break;
        case TypeCode.String:
            oArr[ctr] = RandomString(rnd.Next(100));
            break;
        default:
            oArr[ctr] = 0;
            break;
    }
}
else
{
    oArr[ctr] = getInstance(dllFile, ta.Name);
}
  • 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-15T19:07:44+00:00Added an answer on June 15, 2026 at 7:07 pm

    Generics may be handy here – you could do something like:

    T GetRandomValueByType<T>() where T : IConvertible
    {
       // Compute random...
       return Convert.ChangeType(randomValue, typeof(T));
    }
    

    This would then be called via:

    double value = GetRandomValueByType<double>();
    

    However, this isn’t entirely safe (not that using System.Type is either), as you could still pass a type which implemented IConvertible but was inappropriate.


    Given your comment, I would recommend just making separate methods:

    bool GetRandomBoolean() { // ...
    double GetRandomDouble() { // ...
    int GetRandomInt() { // ...
    

    If you’re already going to switch on the type, and make specific implementations, this provides a cleaner, safer way to handle it. By having separate methods, you eliminate the possibility of passing an incorrect type, and simplify the API.

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

Sidebar

Related Questions

As I was writing this function I knew that I wouldn't get tail call
I am writing a program in which I need to get a random value
I am writing a random question generator. I would like to pick one question
Is there a way to use methods with no return value such as random.shuffle
I am writing a game where the computer chooses a random number that a
Writing a python program, and I came up with this error while using the
I am writing this on an Arduino so I don't believe the full scope
I am trying to generate a random number, but it should not be a
If you are writing a game you should think about cheaters and how to
I am writing a function in R that will compute a sum of squares

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.