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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:54:43+00:00 2026-05-13T23:54:43+00:00

For a part of a program i need the following 2 methods. The first

  • 0

For a part of a program i need the following 2 methods.

The first method listed will generated a random number.
where the 2nd method will “call” this method to fill the array.

The array has a max. number of elements defefined on 100 (and all the random generated numbers should be between 1-100).

The problem is i never get random numbers generated. (either i get 100 x the same value, 3 random numbers divided over the 100 max. elements of the array, or the same value 100 times all over again).

The problem should be in the first method, but i cannot seem to figure out the problem.
Been staring at this for quite some time now…

The problem should be with the return, cause it DOES create random generated numbers. But how do i return the generated value every time? (the int method has to be called with the 3 parameters).

        private int ValidNumber(int[] T, int X, int Range)
    {
        for (byte I = 0; I < T.Lenght; I++)
        {
            Random RndInt = new Random();
            X = RndInt.Next(1, Range+1);
        }
        return X; 

    }/*ValidNumber*/


  public void FillArray(int[] T, int Range)
    {
        for (byte I = 0; I < T.Length; I++)
        {
            T[I] = ValidNumber(T, I, Range);
        }

    }/*FillArray*/

Console code:

public void ExecuteProgram()
    {
        ClsBereken Ber = new ClsBereken();

        //const byte Range = 100;
        const int Max = 100;
        int[] T = new int[Max];

        Ber.FillArray(T, Max);
        DisplayArray(T);

    }/*ExecuteProgram*/

    private void DisplayArray(int[] T)
    {
        for (byte i = 0; i < T.Length; i++)
        {
            Console.Write("{0,4} ", T[i]);
        }
        Console.WriteLine();
    }/*DisplayArray*/

Any help alot appreciated.
Kind Regards.

  • 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-05-13T23:54:44+00:00Added an answer on May 13, 2026 at 11:54 pm

    Re-use the Random instance. NOTE I’ve edited this to show passing the Random instance down, but I’m really not sure what ValidNumber is trying to do – it looks like it is juts burning up CPU cycles? I would suggest you can remove ValidNumber completely (and just use the next value from the Random in FillArray), but presumably you are trying to do something here – I’m just not sure what!

    private int ValidNumber(int[] T, int X, int Range, Random random)
    {
        for (byte I = 0; I < T.Lenght; I++)
        {
            X = random.Next(1, Range+1);
        }
        return X; 
    
    }/*ValidNumber*/
    
    
    public void FillArray(int[] T, int Range)
    {
        Random random = new Random();
        for (byte I = 0; I < T.Length; I++)
        {
            T[I] = ValidNumber(T, I, Range, random);
        }
    
    }/*FillArray*/
    

    When you create a Random, it is “seeded” using the system clock, but this is rounded heavily. If you create lots of Random in a tight loop, they all get the same “seed”, so they all create the same next number.

    If necessary you could move the Random further out (if you have other loops), or make it static (but if you do that you need to worry about synchronization too).

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

Sidebar

Related Questions

I am using in the first part of my program On Error GoTo start
I would like to execute ls in a Perl program as part of a
As part of a VBA program, I have to set the background colors of
We host the .NET runtime as part of a Win32 program, and lately it
Our installation program is written using InstallShield2009, and as part of certification requirements we
Assuming self-registration is used to install components as part of a larger installer program,
This is homework, but I need a nudge. I can't find how to sort
I have the following query. I need Description to be set to the value
I have this project I'm working on. The following conditions apply In this project
A part of my program uses WinInet's caching function (e.g. FindFirstUrlCacheEntry, FindNextUrlCacheEntry) to go

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.