Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

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.  
0
smileinjection
Asked:
smileinjection
  • 5
  • 3
1 Solution
 
PanimuCommented:
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
 
smileinjectionAuthor Commented:
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
 
Anthony PerkinsCommented:
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
smileinjectionAuthor Commented:
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
 
smileinjectionAuthor Commented:
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
 
PanimuCommented:
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
 
smileinjectionAuthor Commented:
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
 
PanimuCommented:
Replace Rand() with

RAND((DATEPART(ss, GETDATE()) * 1000) + DATEPART(ms, GETDATE()))
0
 
smileinjectionAuthor Commented:
oh!  hehe.. see, I dont know much. :)  THanks.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now