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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:33:12+00:00 2026-05-26T05:33:12+00:00

I made this code to load about 8 different textures to a list of

  • 0

I made this code to load about 8 different textures to a list of objects with a texture property.

I have a folder with textures named “1.png, 2.png, 3.png,…….,46.png” and I want the 8 different objects to be loaded with randomly chosen textures.

        DockedFruitsList = new List<Fruit>(8);
        for (int i = 0; i < 8; i++)
        {
            Fruit temp = new Fruit();
            temp = new Fruit();
            temp.Position = AvailablePositions[i];
            int random=(new Random().Next(0, 4600) % 46);
            temp.Texture = Content.Load<Texture2D>(@"Fruits/" + random);
            DockedFruitsList.Add(temp);          

        }

The thing is, despite the random always generates a different number, the result of draw is always the same texture, it changes from a run to another run, but it’s always the same for all the 8 textures

spriteBatch.Begin();
        for (int i = 0; i < DockedFruitsList.Count; i++)
        {
            spriteBatch.Draw(DockedFruitsList[i].Texture, DockedFruitsList[i].Position, Color.White);
        }
        spriteBatch.End();
  • 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-26T05:33:13+00:00Added an answer on May 26, 2026 at 5:33 am

    Try this out:

    for (int i = 0; i < 100; i++)
        Console.WriteLine(new Random().Next(0, 10));
    

    Odds are you’ll get an output like this:

    5
    5
    5
    .
    .
    .
    

    This is because the seed of the Random class is initialized with a time-based value. If you initialize many instances of the Random class near the same point in time, then they will all end up with the same seed, and so the values they will produce will all be the same.

    What you really want to do is to create one Random instance and use that for all your random values. The values generated will have no relation to each other:

    var r = new Random();
    for (int i = 0; i < 100; i++)
        Console.WriteLine(r.Next(0, 10));
    

    Output:

    2
    8
    0
    .
    .
    .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
I have made this code to open a database(created in SQLite browser) stored in
I have a problem, and I made this test code to show you my
I have a line, which is a sprite made by using this code CGPoint
How can I improve this code? What has made this long winded is the
I am pretty new at C# and .NET, but I've made this code to
I've made this piece of code, it will re-size images on the fly, if
Ok. I've made this awe-crap-code Exporting table to XLS (but still no idea how
I made this little function from code snippets around the net. It does what
I didn't have much luck finding out much about this one… I have 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.