Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 31st, 2004, 11:06 PM
simres simres is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 21 simres User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question simple algo going wrong

int k=0;
int n1;

while(pkey3!=0)
{

n1=pkey3%10;
k=k*10+n1*2;
pkey3=pkey3/10;
}


if(k!=0)

{
k=k+1111;
int k1=k%100;
k=k/100;
String k2=" "+k+"SR"+k1;

**********************************************
i am doing a project in jsp n mysql. As a part of it i need to generate a unique string as in the above code say "k2". for this we r taking an input from the jsppage say "pkey3". hope u understand the logic. but the problem i am facing here is for pkey3 values like 10,100,1000,20,200,2000...... and so on the value generated for k2 is the same... it should b unique. . so i need some modifications in the code. so please help me out to correct this code or suggest me a simple algorithm to generate a unique string.....

thanx in advance
regards
simres

Reply With Quote
  #2  
Old April 1st, 2004, 09:39 AM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,306 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 2 h 51 m 33 sec
Reputation Power: 105
Send a message via ICQ to christo Send a message via Yahoo to christo
if you want a unique number, why not use a timestamp - or a rand seeded with a microtime value.

christo

Reply With Quote
  #3  
Old April 1st, 2004, 11:33 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Permanently Banned
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 7,351 DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 19 h 39 m 15 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Varies by implementation, but something like:

srand (time (0)); (seed just once)

then use rand (). can be scaled with the modulus operator.

Reply With Quote
  #4  
Old April 1st, 2004, 12:15 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Quote:
Originally Posted by christo
if you want a unique number, why not use a timestamp - or a rand seeded with a microtime value.

christo


Actually, being that it's mysql, you'd define the field as and "auto increment"

Here's what I use: (taken from some code of mine)

Code:
create table test (
	id int(6) auto_increment primary key,
	name varchar(50) not null,
	dob date
);


Does it need to be a string? Does it need to be predictable from the k2?
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare

Reply With Quote
  #5  
Old April 1st, 2004, 10:46 PM
simres simres is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 21 simres User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hai all,
thanks for the response.but "k2" should be a string itself and it can,t be generated as autoincrement.....
so please help in this regard.
regards
simres

Reply With Quote
  #6  
Old April 2nd, 2004, 02:25 AM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,306 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 2 h 51 m 33 sec
Reputation Power: 105
Send a message via ICQ to christo Send a message via Yahoo to christo
if you're doing this in Java, this class will do the job by the looks of things.

christo

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > simple algo going wrong


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT