-3

I am generating a random number every 5 minutes:

$seed = floor(time()/(60*5));
srand($seed);
$random = rand(2,30);

I would like to randomize the number to either the value 1 or a number between 2 - 30 based on a 50/50 chance?

Is this possible?

user3574492
  • 6,225
  • 9
  • 52
  • 105
  • 2
    first the 50\50 rand, then your other logic, have you tried this? –  Feb 07 '17 at 20:28
  • Do you also mean you will not be generating the same number at any 5 minutes interval? – Wolverine Feb 07 '17 at 20:32
  • " Note: There is no need to seed the random number generator with srand() or mt_srand() as this is done automatically. " –  Feb 07 '17 at 20:40
  • @Perumal93 No, it can be the same. – user3574492 Feb 07 '17 at 20:41
  • @nogad I need to use a seed so I can get a new random number every 5 minute interval. – user3574492 Feb 07 '17 at 20:44
  • no you dont. each call to rand will self seed –  Feb 07 '17 at 20:45
  • 1
    So how will I add the 5 minute interval without a seed? – user3574492 Feb 07 '17 at 20:49
  • 2
    you have a whole bunch of answers below where you didn't comment about/accept, yet you posted this http://stackoverflow.com/a/42099518/1415724 and no idea what you're doing there and not responding to my comment about it. – Funk Forty Niner Feb 07 '17 at 21:05
  • @Fred-ii- Calm down I respond when I see it what do you expect me to do look at it every 2 seconds? – user3574492 Feb 07 '17 at 21:06
  • 2
    Oh I'm calm alright. I'm just wondering why the other people who gave you answers were left in the dark. – Funk Forty Niner Feb 07 '17 at 21:07
  • You sound like you have your panties in a bunch. They were not 'left in the dark' I was simply finding another solution on my own and posted it. How do you know I won't get to responding to them? – user3574492 Feb 07 '17 at 21:09
  • 1
    ok well whatever; open up a chatroom. Like I said, this isn't a BBS service. – Funk Forty Niner Feb 07 '17 at 21:10
  • the time\seed bit is confusing, im not sure you know what a random number seed does. Can you explain what you mean by "how will I add the 5 minute interval" –  Feb 07 '17 at 21:14
  • @nogad I basically want the random number to generate every 5 minutes. That's it. – user3574492 Feb 07 '17 at 21:15
  • ok well that's noting to do with the seed. php will call this code once on load, and never again. If you want a page showing this number to change the number you will need some client side code. you could just write this all in client side code if thats going to be the case –  Feb 07 '17 at 21:16
  • Oh I see, so you're saying that the 5 minute interval won't actually work because it will only run this code whenever the page is loaded? – user3574492 Feb 07 '17 at 21:20
  • exactly, that time code you have does nothing. seeding has noting to do with what you want. php will run this once only no matter what –  Feb 07 '17 at 21:22
  • That is what he said. To run a PHP script every 5 minutes you would use a CRON job. – Jay Blanchard Feb 07 '17 at 21:22
  • i assumed it was for a users view, but if not, then perhaps the answer is cronjob not js –  Feb 07 '17 at 21:23
  • 3
    Name calling will get you nowhere @user3574492. Please refrain from doing so. – Jay Blanchard Feb 07 '17 at 21:23
  • Possible duplicate of [How to run a cronjob every X minutes?](http://stackoverflow.com/questions/25740573/how-to-run-a-cronjob-every-x-minutes) – Funk Forty Niner Feb 07 '17 at 21:31

4 Answers4

1
    $x = rand(0, 1) ? 1 : rand(2,30);
MackProgramsAlot
  • 583
  • 1
  • 3
  • 16
0

Here you go. If the first rand returns a 1 then it will give you the result of rand(2,30) otherwise it will become 1

$random = rand(0,1) == 1 ? rand(2,30) : 1;
skrilled
  • 5,350
  • 2
  • 26
  • 48
0

You can simply randomize between 1 and 58:

  $seed = floor(time()/(60*5));
  srand($seed);
  $random = rand(1,58);
  if ($random > 30) $random = 1;

2..30 are 29 numbers, 1 and 31 ... 58 are also 29 numbers -> 50/50

Tom Kuschel
  • 695
  • 8
  • 19
0
$seed = floor(time()/(60*5));
srand($seed);
$random = rand(1,rand(2,30));
user3574492
  • 6,225
  • 9
  • 52
  • 105