Myanmar IT Resource Forum
Myanmar IT Resource Forum
Myanmar IT Resource Forum

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

alt.332

alt.332
Administrator



Administrator
Hello everybody, I want to share some algorithm for js but it's also useful for other programming languages.... This one is not my own idea. I got this one when I read js programming for absolute beginner(e-book)... It's useful when u want random numbers in certain range....

eg - random number between 1-6 , 4-20, 10-100, etc....

Very simple.
1. Get a random floating point value.
2. Multiply the value by high.
3. Convert the value to integer.
4. Add the value of low.

Example, we want to make some dice program.... The lowest number is 1 & highest is 6...

1st Step - make random floating point value,
eg - var randomNum = Math.random();

2nd Step - multiply with high,
eg - var high = randomNum * 6;

3rd Step - convert the value to integer,
eg - var integer = Math.floor(high);

Last one 4th Step - add the value of low,
eg - var result = integer + 1;

that's all.... it's very simple & very useful.....
here's the sample code...
Code:

<html>
<head>
<title>JS Practice</title>
</head>

<body>

<script>

var rollraw = 0;
var rollBigger = 0;
var rollnt = 0;
var rollFinal = "";

rollraw = Math.random();
rollBigger = rollraw * 6;
rollint = Math.floor(rollBigger);
rollFinal = rollint + 1;

alert("Your number is: " + rollFinal);

</script>

</body>

</html>

http://www.myanmaritresource.info

sHa92

sHa92
Founder



Founder
First of all, Welcome back bro.
I'm glad to see you again here. And now I'm starting to give you awful trouble. Razz

Bro this algorithm is totally wrong. It will never generate random numbers between given Maximum and Minimum numbers. It is only true for random numbers between "1" and "6", not for others.

Let's assume that your lowest number is "3" and your highest number is "6". By your algorithm the minimum(lowest) number must be place at rollFinal = rollint + 3;
right?
If what I understand is right, Please test by that values. Do you got your random number between your Minimum and Maximum numbers? It overflowed your maximum. Smile

OK, let me explain more.
In your step 4, you said that "add the value of low"
Last one 4th Step - add the value of low,
eg - var result = integer + 1;
Why you add that codes? Why you add by 1? The answer is really simple.
I explained in above that the number 1 is not minimum number. So let me explain what the 1 really is.
It is added because we will sometime get "0" value from Math.floor(); and we don't want 0 value. Because we want to generate "1" through "6". So, to avoid that we added with 1.

Your explanation about Math.floor(); is not 100% accurate. Please go to MDC and check about that. (I'll give you the link below)
And also the number "6" which your wrote in "randomNum * 6;" is not really a Maximum(high) number. It is just some tricky. Smile

OK, let's go back to this topic title. It is the Algorithm for Random Numbers. So, here is the real solution.

Code:
// Returns a random integer between min and max
   function getRandomInt(min, max)
   {
     return Math.floor(Math.random() * (max - min + 1)) + min;
   }

This is not my own codes snippet. It is from MDC. I just copied from there.
You said that your code also is not your own version. You just copied from the Book you read and shared for us. I want to suggest you that don't trust the book. Just trust on you. Tweak it. Test it and only Trust your visual result. And also don't trust on me. My answers might wrong. Smile

For more information about JS stuff, you should check MDC.
https://developer.mozilla.org/en/JavaScript

Regards,
sHa92

http://www.myanmaritresource.info

alt.332

alt.332
Administrator



Administrator
yeah... I should test Very Happy .... thanks for ur reply.... I think I don't understand what writer mean exactly... Very Happy or may be writer was wrong Razz

http://www.myanmaritresource.info

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum

 

Make a forum | ©phpBB | Free forum support | Report an abuse | Forumotion.com