# Random Function

Posted by 78185106
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Random Function July 25, 2013 12:37PM Registered: 5 years ago Posts: 56 Rating: 0
Prof Mac,

The dice game in the tutorials where PHP picks a random number of 1-6 and displays the number picked.

I want to know how does PHP know what number to pick and not to pick the same one in a row.

Is it how php is written? how was the Random Function written?

Its just been bugging my mind..

Willem
 Re: Random Function July 25, 2013 04:32PM Registered: 5 years ago Posts: 25 Rating: 0
Well the number picked is random, so it could be picked 500 times in a row.
 Re: Random Function July 25, 2013 04:47PM Admin Registered: 14 years ago Posts: 9,683 Rating: 307
Technically, a good question....!

But also why there exists such an in-built function that we can use without understanding it This is the cornerstone of an OOP-approach.

To truly know we will have to ask the PHP guys, I guess.
 Re: Random Function July 25, 2013 05:54PM Registered: 5 years ago Posts: 56 Rating: 0
Thanks Prof Mac.
 Re: Random Function July 25, 2013 08:11PM Registered: 5 years ago Posts: 25 Rating: 0
That's really interesting, thanks Mac.
 Re: Random Function July 26, 2013 05:09PM Registered: 6 years ago Posts: 73 Rating: 0
kikikikikikikikikikikikikikikikikiki.....
 Re: Random Function July 26, 2013 05:18PM Registered: 6 years ago Posts: 73 Rating: 0
The same function can be done in JavaScript, Observe:
****However I must say PHP's aproach is cleaner, notice the "Math.floor()" function that rounds the result of "Math.random()" to one digit..i.e without the "Math.floor()" outputs something like this: 1.065623, 2.04698 e.t.c
`Language: Javascriptvar \$myNumber = Math.floor((Math.random()*6)+1);`
 Re: Random Function July 31, 2013 01:02PM Registered: 5 years ago Posts: 11 Rating: 0
Great question Willem.

I did some reading on the topic and the most interesting thing I learned is that the algorithm used in this function is close to a true randomised result but is unable to incorporate the God factory which will make it true randomisation.

Thanks man!
 Re: Random Function July 31, 2013 01:21PM Registered: 6 years ago Posts: 73 Rating: 0
I think Prof Mac, left the building??
 Re: Random Function July 31, 2013 02:41PM Admin Registered: 14 years ago Posts: 9,683 Rating: 307
I only respond when it is necessary I am far more interested in using the function than understanding how it works in detail - no need to understand it!
 Re: Random Function July 31, 2013 04:19PM Registered: 6 years ago Posts: 73 Rating: 0
lol.. So the fact that you're quiet means we're doing a great job assisting one another..
 Re: Random Function August 01, 2013 10:45AM Admin Registered: 14 years ago Posts: 9,683 Rating: 307
Yes. And The more support from various people, the more perspectives... Also, by responding to a problem, you also also learn
Sorry, only registered users may post in this forum.