chandra sekhar
chandra sekhar

Reputation: 185

Generating a unique random number

I am entering student id as a randon number into the DB

  int num = r.Next(1000);
  Session["number"] = "SN" + (" ") + num.ToString();

But is there any chance of getting a duplicate number?How can i avoid this?

EDIT :: I have a identity column and the student id is separate from the ID,i am going to enter a random student id into the DB from UI.

Upvotes: 2

Views: 1437

Answers (9)

Scott Selby
Scott Selby

Reputation: 9570

you can use Guid's instead of random int , they are going to always be unique

There is no way to guarentee an int is unique unless you check every one that already exists, and even then - like the comments say , you are guarenteed duplicates when you pass 1000 ids

EDIT:

I mention that I think Guid's are best here because of the question , first indexing the table is not going to take long at all - it is assumed that there are going to be less then 1000 students because of the size of int, 128 bits is fine in a table with less then 1000 rows.

Guid's are a good thing to learn - even though they are not always the most effecient way

Creating a unique Guid in c# has a benifit that you can keep using and displaying that id - like in the question , without another trip to Db to figure out which unique id was assigned to the student

Upvotes: 3

In LINQ to SQL it is possible to set row like this:

    [Column ( IsPrimaryKey = true, IsDbGenerated = true )] 
    public int ID { get; set; }

I dont know if it helps you in asp, but maybe it is a good hint...

Upvotes: 1

Servy
Servy

Reputation: 203802

It is a very common task to have a column in a DB that is merely an integer unique ID. So much so that every database I've ever worked with has a specific column type, function, etc. for dealing with it. It will vary based on whatever specific database you use, but you should figure out what that is and use it.

  • You need a value that is unique not, random. The two are different. Random numbers repeat, they aren't unique. Unique numbers also aren't random. For example, if you just increment numbers up from 0 it will be unique, but that's not in any way random.
  • You could use a GUID, which would be unique, but it would be 128 bits. That's pretty big. Most databases will just have a counter that they increment every time you add an item, so 32 bits is usually enough. This will save you a lot of space. Incrementing a counter is also quicker than calculating a GUID's new value. For DB operations that tend to involve adding lots of items, that could matter.
  • As Jodrell mentions in the comments, you should also consider the size of the index if you use a GUID or other large field. Storing and maintaining that index will be much more expensive (in both time and space) with column that needs that many more bits.
  • If you try to do something yourself there's a good chance you'll do it wrong. Either your algorithm won't be entirely unique, it will have race conditions due to improper synchronization, it will be less performant because of excessive synchronization, it will be significantly larger because that's what it took to reduce the risk of collisions, etc. At the end of the day the database will have access to tools that you don't; let it take care of it so you don't need to worry about what you could mess up.

Upvotes: 7

Steve Baer
Steve Baer

Reputation: 101

Sure there is a very likely chance that you will get a duplicate number. Next is just giving you a number between 0 and 1000, but there is no guarantee that the number will not be some number that Next has returned in the past.

If you are trying to work with unique values, look into possibly using Guids instead of integers or have a constantly increasing integer value instead of any random number. Here the reference page on Guid http://msdn.microsoft.com/en-us/library/system.guid.aspx

Upvotes: 3

SHSE
SHSE

Reputation: 2433

You can try to generate id using Guid:

Session["number"] = "SN" + (" ") + Guid.NewGuid().ToString();

It will highly descrease a chance to get duplicate id.

Upvotes: 2

Jodrell
Jodrell

Reputation: 35696

If you are using random numbers then no there is no way of avoiding it. There will always be a chance of a collision.

I think what you are probably looking for is an Identity column, or whatever the equivalent is for your database server.

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

Yes, you will certainly get duplicates. You could use a GUID instead:

Guid g = Guid.NewGuid();

GUIDs are theoretically "Globally Unique".

Upvotes: 2

BlakeH
BlakeH

Reputation: 3484

Yes, you will get duplicates. If you want a truly unique item, you will need to use Guid. If you still want to use numbers, then you will need to keep track of the numbers you have already used, similar to identity column in database.

Upvotes: 2

Lucas Moeskops
Lucas Moeskops

Reputation: 5443

Yes there is a chance of course.

Quick solution:
Check if it is a duplicate number first and try again until it is no longer a duplicate number.

Upvotes: -1

Related Questions