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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:53:23+00:00 2026-05-25T06:53:23+00:00

So I have method that generates a barcode and label with that barcode. We

  • 0

So I have method that generates a barcode and label with that barcode. We have noticed that when more than one user generate a barcode at the same time they will both get the same file. I am using ASP.NET and I am hosting the application and the file on the a in house server.

public void trickylabel(string fnsku, string title)
{
    Random random = new Random();
    int randomNumber = random.Next(0, 100000);

    //Set barcode properties...

    code.parse(fnsku); // Text

    BCGDrawing drawing = new BCGDrawing(this.Server.MapPath("~") + "image"+ randomNumber.ToString() +".png", color_white);
    drawing.setBarcode(code);
    drawing.draw();

    // Draw (or save) the image into PNG format.
    Response.ContentType = "image/png";
    drawing.finish(ImageFormat.Png);

    Document doc = new Document(new iTextSharp.text.Rectangle(200f, 75f), 20F, 10F, 10F, 1F);
    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath +
        "\\"+randomNumber.ToString()+".pdf", FileMode.Create));

    doc.Open();

    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(this.Server.MapPath("~") + "image" + randomNumber.ToString() +".png");

    doc.Add(png);

    //Sets pdf properties...

    doc.Add(new Paragraph(title, times));
    PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
    writer.SetOpenAction(action);
    doc.Close();

    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=labels.pdf");
    Response.TransmitFile(Server.MapPath("~/"+randomNumber.ToString()+".pdf"));

}
  • 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-25T06:53:23+00:00Added an answer on May 25, 2026 at 6:53 am

    The problem is here:

    Random random = new Random();
    int randomNumber = random.Next(0, 100000);
    

    The Random class generates pseudo-random sequence, that is based on some starting value, called Seed. If two Random instances are initialized with equal seeds they will produce equal number sequences.

    When Random is created with parameterless constructor the Seed is created from current system time. If you ran a loop that creates instances of Random class you will notice that random.Next() value changes only several times per second (once per 16ms).

    To avoid this you should reuse initialized Random instance (however, since you are using ASP.Net you will need to ensure that the calls of random.Next() are executed in a thread-safe way). Then your visitors will get different values each new random.Next(…) call.

    Alternatively Random has a constructor that allows to set the initial Seed value manually. So, you can create your own algorithm of creating unique seeds for your users.

    UPDATE Thread-safe Randomizer implementation.
    Gets initialized on assembly load.

    Usage: just replace random.Next(0, 100000); to Randomizer.Next(0, 100000);

    public static class Randomizer
    {
        private static Random rnd;
        static Randomizer()
        {
            rnd = new Random();
            rndlock = new object();
        }
    
        private static object rndlock;
        public static int Next(int minValue, int maxValue)
        {
            lock(rndlock)
            {
                return rnd.Next(minValue, maxValue);
            }
        }
    }
    

    UPDATE On thread safety and unpredictable results

    As stated above, Random generates pseudo-random sequence. This means that each int number in this sequence has known previous and next numbers. This means that after a certain number of calls these numbers will start to repeat. The Random.Next() algorithm is designed to maximize number of unique items in this sequence.

    So, what “unpredictable” means in this case? When several threads use the same variables simultaneously the logic of the algorithm becomes corrupted. In best scenario this makes sequences of same numbers occur more often. In worst scenario Random.Next() starts to produce zero values on each call without any chance to recover.

    More useful information about System.Random on MSDN.

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

Sidebar

Related Questions

I have a method that where I want to redirect the user back to
I have a method that generates and error that a int was expected but
In Java, a for-each loop. If I have a method that generates an array,
I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I have a method that generates XML for a class I have created -getXML().
Context: C# 3.0, .Net 3.5 Suppose I have a method that generates random numbers
Ok, so I have an action method that generates a PDF and returns it
I have 2 sets of XSD's, one that generates RPC based calls, and another
I have a method on one of my objects that returns a new instance
I have class method that returns a list of employees that I can iterate

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.