Jump to content


random ()


3 replies to this topic

#1 evertonian7uk

    Young Padawan

  • Members
  • Pip
  • 249 posts

Posted 17 November 2006 - 09:02 AM

hi guys,

i am trying to make a few things appear random in my game, i thought the below code was good enough, but the random items are appearing far to quickly,

what i want is items to appear at very large intervials, im talking days before anyone see's one's for testing i made the code

Quote

$randevent=rand(0,1500);
$event="";
if($randevent==0){ $rare1=1; $event="Your villagers have uncovered the eye of evil stone..."; }
if($randevent==250){ $rare2=1; $event="Your villagers have uncovered the jewel of death stone..."; }
if($randevent==350){ $rare3=1; $event="Your villagers have uncovered the stone of fear..."; }
if($randevent==450){ $rare4=1; $event="Your villagers have uncovered the dragons eye stone..."; }
if($randevent==550){ $rare5=1; $event="Your villagers have uncovered the statue of eden..."; }
if($randevent==650){ $rare6=1; $event="Your villagers have uncovered the priest of lier stone..."; }
if($randevent==750){ $rare7=1; $event="Your villagers have uncovered the sphere of sin stone..."; }
if($randevent==850){ $rare8=1; $event="Your villagers have uncovered the knights of satan stone..."; }
if($randevent==950){ $rare9=1; $event="Your villagers have uncovered the weather witch stone..."; }
if($randevent==1500){ $rare10=1; $event="Your villagers have uncovered the gods of war stone..."; }


i figured at 1500 it would take a while to find them all, but i had them within 10 mins, can someone point out my mistake?? or give me a little advice on how to lengthen the returns.

as always thanks a mill for the help.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 November 2006 - 04:32 PM

I assume this is in PHP?

Well, 1,500 isn't that large a number to be randomizing from, really. What I would do, is generate a completely random number, based on rand()'s native range, combined maybe a few times.
Do a simple search for 'random' in the PHP area of P2l here, and you'll find plenty of good 'algorithms' I guess you could call them, but this would be a sample of something you could do.

<?php
srand((double)microtime()*1000000);
$randevent = mt_rand();
// In this case, switch would be more convenient, and will keep you from having to waste memory with several ifs, since a number can only be equivelent to one number at a time...
switch($randevent){
	case 0: $rare1 = 1; $event = 'Your villagers have uncovered the eye of evil stone...'; break;
	case 250: $rare2 = 1; $event = 'Your villagers have uncovered the jewel of death stone...'; break;
	case 350: $rare3 = 1; $event = 'Your villagers have uncovered the stone of fear...'; break;
	case 450: $rare4 = 1; $event = 'Your villagers have uncovered the dragons eye stone...'; break;
	case 550: $rare5 = 1; $event = 'Your villagers have uncovered the statue of eden...'; break;
	case 650: $rare6 = 1; $event = 'Your villagers have uncovered the priest of lier stone...'; break;
	case 750: $rare7 = 1; $event = 'Your villagers have uncovered the sphere of sin stone...'; break;
	case 850: $rare8 = 1; $event = 'Your villagers have uncovered the knights of satan stone...'; break;
	case 950: $rare9 = 1; $event = 'Your villagers have uncovered the weather witch stone...'; break;
	case 1500: $rare10 = 1; $event = 'Your villagers have uncovered the gods of war stone...'; break;
	default: break;
}
?>

Hope that helps. :)

#3 evertonian7uk

    Young Padawan

  • Members
  • Pip
  • 249 posts

Posted 18 November 2006 - 05:52 AM

i checked out the searches and tried a few things but still i kept getting my returns to quickly,

The problem is i dont understand the rand function, i checked out PHP.net and couldnt understand it to much.

a few say change the microtime number and some dont,


i used yours above and the returns were just as quick, i did a google for php random functions but you get so much crap to search tthrough aswell.

is there an idiots guide somewhere to randoms? i am only learning as i go on, and this has proven to be the stumbling block so far, using tuts here ive made logins, attack systems and others but random is a bugger LOL!!

thanks a mill for the help so far :)

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 18 November 2006 - 11:43 AM

Well, if you wanted a truly random event, you should mix it with other characters and such, such as this.

$random = strtoupper(substr(md5(uniqid(mt_rand(),true)),0,6));
// Then use some method of generating other random strings to match against
switch($random){
case strtoupper(substr(md5(uniqid(mt_rand(),true)),0,6)): echo 'Event 1'; break;
case strtoupper(substr(md5(uniqid(mt_rand(),true)),0,6)): echo 'Event 2'; break;
}

However I would bet that would be quite a strain on the server to load all of the time.
Just get creative and create your own basis for generating and checking randomly produced strings.
What I would personally do is create a function to do this, and figure out a way to make it make a string based on difficulty, such as 1 being easily possible, and could maybe happen every now and then, to 10, being ultra-rare and probably hardly anyone would ever get it but with great luck. Then you could simply call it like this, and generate as many keys as you need with a for loop.
$random = generate_random(10);

# You could loop through and generate random difficulties
for($i = 1; $i < 4; $i++){ // Run 3 times
$event[$i] = generate_random(mt_rand(0, 10));
}

# OR you could set the difficulties for each event as you wish
$event[1] = generate_random(2);
$event[2] = generate_random(4);
$event[3] = generate_random(10);

print_r($event);

Look more into the security images and CAPTCHA as they produce pretty reliable random strings.

Edited by Demonslay, 18 November 2006 - 11:50 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users