9 Replies - 751 Views - Last Post: 11 July 2011 - 07:39 AM Rate Topic: -----

#1 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 07:16 PM

First off, I know you don't need to call the srand() function but I use a seed so I can duplicate results if needed. The normal functions use rand() without the seed.

Anyway, my problem simplified is this: different strings -> sha256 hash -> srand() = same output?

It doesn't happen with every string, but sometimes I will get the same output as other times, but it's not a common occurrence. I have looked over my code to see if I could find what might be causing it (albiet not as deep as I probably should have) but I noticed that it could be that my script is calling the srand() function multiple times. I didn't think anything of this until I found this on the w3schools site:

"This function should be called only once per script, and it must be called before any calls to rand()."

I'll have to check the flow of my code again but I'm wondering if the calling it multiple times may cause this...?

Is This A Good Question/Topic? 0
  • +

Replies To: What happens if you call srand() more than once in a script?

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 07:32 PM

Quote

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
Even if you don't call it it's called automatically. You only get the same hash output if the input is the same. Meaning that if randgenerates 100 then later on generates it again the hashed string will be the same.

What is your goal? Provide us with source code so we can give a better and more comprehensive answer because I'm still not getting what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#3 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Re: What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 07:49 PM

[quote name='codeprada' date='10 July 2011 - 08:32 PM' timestamp='1310351525' post='1383761']

Quote

What is your goal? Provide us with source code so we can give a better and more comprehensive answer because I'm still not getting what you're trying to do.


I don't have access to the source at the moment (and it's a tad bulky to post here) but here is the working product at the moment: passwordchart.net/?page=settings

If you put in the words "test" and "moss" you will get the same results (2g*R for A). I shouldn't be getting the same hash because I'm using SHA256. There haven't been any known collisions with SHA1 let alone SHA2. Here is the process that the input string goes through:

  • Input string converted (url-safe entities/whitespace replaced with "+")
  • New string sent to SHA256 algo
  • Hash sent to srand() function

Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 07:58 PM

Wait what? Hash sent to srand() function? srand accepts an integer so if you send a string it may very well be converted to an integer which in this case will be 1.
Was This Post Helpful? 1
  • +
  • -

#5 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Re: What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 08:15 PM

View Postcodeprada, on 10 July 2011 - 08:58 PM, said:

Wait what? Hash sent to srand() function? srand accepts an integer so if you send a string it may very well be converted to an integer which in this case will be 1.


I think I might have done some other converting to the hash now that I think about it. If not, this could be my answer. I'll post more once I get home to check the code.
Was This Post Helpful? 0
  • +
  • -

#6 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Re: What happens if you call srand() more than once in a script?

Posted 10 July 2011 - 11:41 PM

You were right in that it was the hash, but what I was doing was (int)$hash, which was giving me a much shorter number than it really was, which in turn was giving me duplicate outputs due to less possilble number combinations. The problem I guess I'm running into is that the number I'm generating is too huge to convert to an integer?

This code:
function randomSeedGet($hashSeed,$hashAlgo="sha256") {
	$randSeed = hash($hashAlgo,$hashSeed);
	return $randSeed;
}
if (isset($_POST['hashSeed'])) {
	$hashSeed = $_POST['hashSeed'];
	$hashSeed = str_replace(" ", "+", $hashSeed);
	$temp = base_convert(randomSeedGet($hashSeed), 16, 10);
	$randSeed = (int)$temp;
	// added the following for descriptive purposes
	echo $temp."<br/>";
	echo intval($temp)."<br>";
	echo (int)$temp."<br>";
	echo $temp+0 ."<br>";
	echo (float)$temp."<br>";
}

Gives me this:

Quote

3278806482024048262808804284242402800066220486262222448688206200
2147483647
2147483647
3.278806482024E+63
3.278806482024E+63

Any suggestions on how to get this huge number into srand()?

This post has been edited by goku87: 10 July 2011 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Re: What happens if you call srand() more than once in a script?

Posted 11 July 2011 - 12:48 AM

Problem solved... I guess. This is me not knowing my numbers. <_< This problem is impossible. Thanks for the help though; I appreciate it.

http://en.wikipedia....uter_science%29
Was This Post Helpful? 0
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: What happens if you call srand() more than once in a script?

Posted 11 July 2011 - 06:49 AM

Are you trying to produce a random string of characters or a random number?
Was This Post Helpful? 0
  • +
  • -

#9 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Re: What happens if you call srand() more than once in a script?

Posted 11 July 2011 - 07:27 AM

View Postcodeprada, on 11 July 2011 - 07:49 AM, said:

Are you trying to produce a random string of characters or a random number?

I am trying to get a reproducible number from a string input by the user (words are easier to remember than numbers). I've worked something out that seems to be sufficient for my purposes:
## ----- get hash code for random seed ----- ##
if (isset($_POST['hashSeed'])) {
	$hashSeed = $_POST['hashSeed']; // get string to convert to hash
	$hashSeed = str_replace(" ", "+", $hashSeed); // remove spaces
	$hash = hash("sha256", $hashSeed); // get a hash with the 'hash seed'
	$hashShort = substr($hash, -8); // shorten to the last 8 digits
	$temp = base_convert($hashShort, 16, 10); // convert hex to dec
	$randSeed = $temp - 2147483647; // shift to zero-center
}
It's slightly obtrusive as it could be shortened, but this is nice, clean, and easy to follow. Since this takes the last 8 digits of the hash, it should never be more than 4294967295 (FFFFFFFF). Subtracting 2147483647 from this value gives me maximum buffer in both directions (positive and negative from 0) without passing the integer limit, so I can then use $randSeed as an integer in the srand() function.

Sleeping on a problem really does help.

This post has been edited by goku87: 11 July 2011 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: What happens if you call srand() more than once in a script?

Posted 11 July 2011 - 07:39 AM

To make your life a whole lot easier my suggestion is since you said from a string input by the user I'm guessing we're talking registered users here which means a database is involved (correct me if I'm wrong). You can store the users string and matching number in a database. That way we are absolutely positive we won't get duplicates.

This post has been edited by codeprada: 11 July 2011 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1