Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,971 members, 7,817,849 topics. Date: Saturday, 04 May 2024 at 09:10 PM

Simple PHP Quiz: Kill The Rand() - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple PHP Quiz: Kill The Rand() (1930 Views)

Please Help Me Write This Simple Php Switch Statement / Simple Php Challenge / Simple Php Quiz: Why Will This Code Not Work If You Are Uploading Files? (2) (3) (4)

(1) (Reply) (Go Down)

Simple PHP Quiz: Kill The Rand() by DualCore1: 9:52am On Aug 10, 2012
Look at the following code:

<?php
echo rand(1,100);
?>

Everytime this code is run, it shows a number randomly chosen between 1 and 100.
Now kill the randomness. Add some (simple) stuff to the code above so that anytime the code is run it outputs the very same number chosen between 1 and 100.

This is totally senseless for a real life scenario but we can learn a thing or two about PHP's randomness from this.

Note: Just add to the code, don't remove cheesy
For simplicity I used rand().... the whole thing can also be achieved (even better) with mt_rand().
Re: Simple PHP Quiz: Kill The Rand() by spikesC(m): 10:24am On Aug 10, 2012
angry
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 10:57am On Aug 10, 2012
spikes C: angry
Bros are we okay? tongue
Re: Simple PHP Quiz: Kill The Rand() by spikesC(m): 11:13am On Aug 10, 2012
Dual Core:
Bros are we okay? tongue
How can u give this kind of quiz by this time angry
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 11:16am On Aug 10, 2012
spikes C:
How can u give this kind of quiz by this time angry
I still don't understand where you are coming from o. Would you like to explain?
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 2:55pm On Aug 10, 2012
Dual Core: Look at the following code:

<?php
echo rand(1,100);
?>

Everytime this code is run, it shows a number randomly chosen between 1 and 100.
Now kill the randomness. Add some (simple) stuff to the code above so that anytime the code is run it outputs the very same number chosen between 1 and 100.

This is totally senseless for a real life scenario but we can learn a thing or two about PHP's randomness from this.

Note: Just add to the code, don't remove cheesy
For simplicity I used rand().... the whole thing can also be achieved (even better) with mt_rand().

Not impossible. Are there any rules on how to add to the code? Because I can manipulate the output to produce the same result without altering the rand() function. Just by getting whatever rand() outputs and modifying it to always produce same final result. What do you think? Let me know.
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 9:19pm On Aug 10, 2012
I asked for a simple solution but any solution you have to this is fine... we could learn a thing or two from your solution.
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 12:10am On Aug 11, 2012
<?php 
session_start();
!isset($_SESSION['r'])?$_SESSION['r']=0:"";
echo $_SESSION['r']===0?$_SESSION['r']=rand(1,100):$_SESSION['r'];
?>

this uses session to maintain persistence.
other approach is welcomed
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 7:48am On Aug 11, 2012
No comprendo. . .
Re: Simple PHP Quiz: Kill The Rand() by sisqology(m): 11:01am On Aug 11, 2012
Dual Core: Look at the following code:

<?php
echo rand(1,100);
?>

Everytime this code is run, it shows a number randomly chosen between 1 and 100.
Now kill the randomness. Add some (simple) stuff to the code above so that anytime the code is run it outputs the very same number chosen between 1 and 100.

This is totally senseless for a real life scenario but we can learn a thing or two about PHP's randomness from this.

Note: Just add to the code, don't remove cheesy
For simplicity I used rand().... the whole thing can also be achieved (even better) with mt_rand().



<?php
$new = rand(1, 100);
For ($x=1; $x <= 100; $x++)
{
Echo $new;
Echo "<br>";
}
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 1:24pm On Aug 11, 2012
All of una don dey madt! I wan join Afam4ever go compete for the next olympic o jare.
Re: Simple PHP Quiz: Kill The Rand() by spikesC(m): 1:47pm On Aug 11, 2012
*dhtml:
All of una don dey madt! I wan join Afam4ever go compete for the next olympic o jare.
Olympic ni, wetin dey sup for there grin
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 2:01pm On Aug 11, 2012
Thats what is happening - I Will Compete In The Next Olympics - Sports - Nairaland

@Dual core dey show imself abi. . .
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 4:28pm On Aug 11, 2012
*dhtml:
Thats what is happening - I Will Compete In The Next Olympics - Sports - Nairaland

@Dual core dey show imself abi. . .

you think say na code dem dey run there?
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 6:49pm On Aug 11, 2012
webdezzi:

you think say na code dem dey run there?
Na leg i wan take run there, and i hear them go soon add LUDO game to the olympics.
Re: Simple PHP Quiz: Kill The Rand() by quadrillio(m): 12:00am On Aug 12, 2012
is the quiz still open or its closed till the next olympics?

It's been a while, how's everyone in the house?

- Safe
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 7:30am On Aug 12, 2012
The quiz is still opened, but me i no fit do this quiz, the only quiz wey i dey try solve now na - dollars X olympic X dhtml X plenty = too-much-money/retirement
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 10:53am On Aug 12, 2012
sisqology:



<?php
$new = rand(1, 100);
For ($x=1; $x <= 100; $x++)
{
Echo $new;
Echo "<br>";
}

Nice effort, I haven't run this code but from looking at it I can say this will create 99 lines of the same random number and when I refresh it will create 99 lines of another randomly chosen number. The probability of it being the same number as the first is 1/99 which is 0.01.
So this isn't a solution to the quiz.




The other example using sessions, thanks for your approach.

Any other approaches to this?
This na our own olympics.

1 Like

Re: Simple PHP Quiz: Kill The Rand() by databoy247(m): 2:06pm On Aug 12, 2012
wetin be the gold prize?
Re: Simple PHP Quiz: Kill The Rand() by nollyj: 2:19pm On Aug 12, 2012
This is the simple way I can think of but there might be other ways to solve it.


<?php
srand(10);
$rand = rand(1, 100);
echo $rand;
?>
Re: Simple PHP Quiz: Kill The Rand() by bakenda(m): 4:55pm On Aug 12, 2012
This na jamb question o.

If there's a way of 'freezing' time on the testing server,
then the function will return a constant output,
haven't figure out how to do that yet.
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 7:39pm On Aug 12, 2012
I will answer the quiz when i get back from the next olympic game.
Re: Simple PHP Quiz: Kill The Rand() by amebopost(m): 8:28pm On Aug 12, 2012
<?php 
$CONSTANT_OUTPUT = 7;
echo rand(1,100) ? $CONSTANT_OUTPUT : $CONSTANT_OUTPUT;
?>
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 8:46pm On Aug 12, 2012
nollyj: This is the simple way I can think of but there might be other ways to solve it.


<?php
srand(10);
$rand = rand(1, 100);
echo $rand;
?>

This is the best solution.
The aim of the quiz was to create the awareness of creating a seed before using the php random functions.

The best way to create a seed is by using php's microtime function as the seed as this is a fast changing number, something like:

srand((double)microtime() * 1000000)
and
mt_srand((double)microtime() * 1000000)

for rand() and mt_rand() respectively.


Thanks all for your input and respect to the guys who posted solutions with the ternary operator. I am glad my life doesn't depend on the ternary operator (yet).
Re: Simple PHP Quiz: Kill The Rand() by amebopost(m): 9:00pm On Aug 12, 2012
Dual Core:

nollyj:


<?php
srand(10);
$rand = rand(1, 100);
echo $rand;
?>

This is the best solution.

Indeed it is.
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 11:50am On Aug 13, 2012
Oya take your medal, Nolly J!
Re: Simple PHP Quiz: Kill The Rand() by nollyj: 12:47pm On Aug 13, 2012
Dual Core: Oya take your medal, Nolly J!

It is just a learning process. Doing something I have not thought of before

Thanks at Dual Core
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 4:02pm On Aug 13, 2012
nollyj: This is the simple way I can think of but there might be other ways to solve it.


<?php
srand(10);
$rand = rand(1, 100);
echo $rand;
?>

Cool.


<?php


$x = rand(1,100);

$y = (md5(20))*($x/$x)+1;

echo $y;
?>

Good quiz by Dual Core but I really don't know the importance of it. If you output the randomly generated number, you can easily tweak it to become a constant number like the above. However, why get a constant number from rand(), when you can easily just use any number of your choice if a constant number is what you want?

Well, good quiz anyway.
Re: Simple PHP Quiz: Kill The Rand() by DualCore1: 5:06pm On Aug 13, 2012
sunical: Good quiz by Dual Core but I really don't know the importance of it.

Dual Core:
This is totally senseless for a real life scenario but we can learn a thing or two about PHP's randomness from this.

Dual Core:
The aim of the quiz was to create the awareness of creating a seed before using the php random functions.

nollyj:
It is just a learning process. Doing something I have not thought of before
Re: Simple PHP Quiz: Kill The Rand() by Nobody: 10:18pm On Aug 13, 2012
To the olympics ====>
Re: Simple PHP Quiz: Kill The Rand() by omni1: 9:58pm On Aug 14, 2012
I only want to welcome 2 good old friends here.

Welcome Quadrillion and Webdezzi.

Happy to see you guys in here shocked

(1) (Reply)

Which IC Is The Most Popular In The World? / Create Your Own Software Without Programming / MMM Calculator (mavro) - Android App

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 37
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.