?
Solved

How to Seed Rand( ) With A Timestamp

Posted on 2003-02-26
9
Medium Priority
?
1,219 Views
Last Modified: 2008-02-01
I run a traffic exchange script on an SQL server.  I have noticed that it does not randomize the sites very well and I heard that i can fix this by "seeding rand with a timestamp."  I don't know much about sql so I dont know what that means.  I can administer it with PHPmyAdmin.  If anyone can help me out with this, I would be very grateful.  
0
Comment
Question by:smileinjection
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 

Expert Comment

by:Panimu
ID: 8025316
Rand takes a value between 0 and 1 for a seed. If you don't give it one it has some of its own. If you use the same seed it can end up giving the same values.

So by using the time as a seed you can come close to guaranteeing your code provides a new seed each time. So use GETDATE to pull out sections of the current time and feed it to Rand

SELECT RAND((DATEPART(ss, GETDATE()) * 1000)
+ DATEPART(ms, GETDATE()))

So this example uses the current seconds and milliseconds to generate a rand.
0
 

Author Comment

by:smileinjection
ID: 8025714
Ok, looks good.. but how do I get to where I can edit that function using PHPMyAdmin?  Or.. is it in a text file that i could get via ftp?  I don't want to mess this up because I have many people using my script and I don't want any downtime.  Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8025964
A word of caution from BOL when using Rand in a Select:

<quote>
When you use an algorithm based on GETDATE to generate seed values, RAND can still generate duplicate values if the calls to RAND are made within the interval of the smallest datepart used in the algorithm. This is especially likely if the calls to RAND are included in a single batch. Multiple calls to RAND in a single batch can be executed within the same millisecond, which is the smallest increment of DATEPART. In this case, incorporate a value based on something other than time to generate the seed values.
</quote>

Also, don't forget this question:
Scanning through ip ranges in Visual Basic.  Net Send
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20495079.html

Anthony
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:smileinjection
ID: 8026226
Well, you can check out my script at smileinjection.com/exchange.  It wont matter if it generates duplicates.. I just want it to be more randomized than it is now... I jsut dont know how to do it using shared hosting and phpmyadmin.
0
 

Author Comment

by:smileinjection
ID: 8026313
Well, you can check out my script at smileinjection.com/exchange.  It wont matter if it generates duplicates.. I just want it to be more randomized than it is now... I jsut dont know how to do it using shared hosting and phpmyadmin.
0
 

Expert Comment

by:Panimu
ID: 8027194
I'd have to see the actual script source (which may not be wise to release, depends how much you care about it). Can't judge much from seeing the HTML output only.
0
 

Author Comment

by:smileinjection
ID: 8027333
Well, can you just tell me how to put that code in my Rand() function?  I have access too phpMyAdmin, ssh, and ftp.  Thanks.
0
 

Accepted Solution

by:
Panimu earned 400 total points
ID: 8027533
Replace Rand() with

RAND((DATEPART(ss, GETDATE()) * 1000) + DATEPART(ms, GETDATE()))
0
 

Author Comment

by:smileinjection
ID: 8027580
oh!  hehe.. see, I dont know much. :)  THanks.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question