Link to home
Start Free TrialLog in
Avatar of smileinjection
smileinjection

asked on

How to Seed Rand( ) With A Timestamp

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.  
Avatar of Panimu
Panimu

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.
Avatar of smileinjection

ASKER

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
Avatar of Anthony Perkins
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
https://www.experts-exchange.com/questions/20495079/Scanning-through-ip-ranges-in-Visual-Basic-Net-Send.html

Anthony
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.
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.
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.
Well, can you just tell me how to put that code in my Rand() function?  I have access too phpMyAdmin, ssh, and ftp.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Panimu
Panimu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh!  hehe.. see, I dont know much. :)  THanks.