Welcome! Log In Create A New Profile

Advanced

Random Function

Posted by 78185106 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Random Function
July 25, 2013 12:37PM
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
Well the number picked is random, so it could be picked 500 times in a row.
avatar
Mac
Re: Random Function
July 25, 2013 04:47PM
Technically, a good question....!

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

Read this: http://programmers.stackexchange.com/questions/109724/how-do-random-number-generators-work

To truly know we will have to ask the PHP guys, I guess.
Re: Random Function
July 25, 2013 05:54PM
Thanks Prof Mac. smile
Re: Random Function
July 25, 2013 08:11PM
That's really interesting, thanks Mac.
avatar Re: Random Function
July 26, 2013 05:09PM
kikikikikikikikikikikikikikikikikiki.....grinning smiley
avatar Re: Random Function
July 26, 2013 05:18PM
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: Javascript
var $myNumber = Math.floor((Math.random()*6)+1);
eye rolling smiley
Re: Random Function
July 31, 2013 01:02PM
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!smile
avatar Re: Random Function
July 31, 2013 01:21PM
I think Prof Mac, left the building?? eye rolling smiley
avatar
Mac
Re: Random Function
July 31, 2013 02:41PM
I only respond when it is necessary smiling smiley I am far more interested in using the function than understanding how it works in detail - no need to understand it!
avatar Re: Random Function
July 31, 2013 04:19PM
lol.. So the fact that you're quiet means we're doing a great job assisting one another.. smileys with beer
avatar
Mac
Re: Random Function
August 01, 2013 10:45AM
Yes. And The more support from various people, the more perspectives... Also, by responding to a problem, you also also learn smiling smiley
Sorry, only registered users may post in this forum.

Click here to login