Want a random code
Moderator: General Moderators
-
hesham2012
- Forum Newbie
- Posts: 8
- Joined: Fri Mar 09, 2007 11:34 pm
Want a random code
i have a database table contains many numbers from 1 : 200 a want to generate a random code between 1 : 200 but this code must be different than others in database !how come?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
There are several ways to go about this, ranging in complexity and performance.
One simple, easy way of implementing this is simply generating a number from 1 to 200 and then doing it again if you get a unique key violation. Of course, you run a very high risk of collision once you start generating an appreciable amount of numbers.
You could also select all rows from the database, invert it, and generate a random key from that set.
I wonder why 200 though: that's very small, and it'll be filled up in no time.
One simple, easy way of implementing this is simply generating a number from 1 to 200 and then doing it again if you get a unique key violation. Of course, you run a very high risk of collision once you start generating an appreciable amount of numbers.
You could also select all rows from the database, invert it, and generate a random key from that set.
I wonder why 200 though: that's very small, and it'll be filled up in no time.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US