Random Number Generator in C# Using System.Random

  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    Generator Random
Click For Summary
SUMMARY

The discussion focuses on implementing a random number generator in C# using the System.Random class. A key issue identified is the accessibility of the method NoBoundsRandoms, which is marked as private by default. The solution involves changing the access modifier to public, allowing the method to be called from the Main method. Additionally, the seed parameter must be correctly passed to the NoBoundsRandoms method to avoid errors.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with the System.Random class
  • Knowledge of access modifiers in C#
  • Basic concepts of method parameters in C#
NEXT STEPS
  • Learn about C# access modifiers and their implications
  • Explore the System.Random class and its methods in detail
  • Investigate error handling techniques in C#
  • Study best practices for method design in C#
USEFUL FOR

C# developers, software engineers, and anyone interested in implementing random number generation in their applications.

TheDemx27
Gold Member
Messages
169
Reaction score
13
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public class RandomNextDemo
        {
            static void NoBoundsRandoms(int seed)
            {
                Console.WriteLine(
                    "\nRandom object, seed = {0}, no bounds:", seed);
                Random randObj = new Random (seed);
            }
        }
        
        static void Main(string[] args)
        {
            RandomNextDemo.NoBoundsRandoms();

        }  
    }
}

Error1'ConsoleApplication1.Program.RandomNextDemo.NoBoundsRandoms(int)' is inaccessible due to its protection

In C#, of course.
 
Technology news on Phys.org
Change the access modifier: static public void NoBoundsRandoms(int seed) by default it would set the access modifier to private which is what your current code does.
 
Last edited:
MathWarrior said:
Change the access modifier: static public void NoBoundsRandoms(int seed) by default it would set the access modifier to private which is what your current code does.

Thanks, I guess I also forgot to give the method the value "seed" it called for.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
894
Replies
22
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
1K