Generating random numbers is very important in programming. It’s difficult as much as it’s important. Actually, there is nothing random. A number, which is choosen according to some criterias (mostly time), is used in some functions, then a random number is given (for us) by those functions…
Let’s start.
Structure 1.1: We will need three libraries. (stdio.h,stdlib.h and time.h)
1 2 3 4 5 6 7 8
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
main(){
srand((unsigned)time(0)); /* this function is used to generate a different number for each time we call rand() function */
rand() % MaxNum; /* The MaxNum is our maximum limit. */
} |
Formula: 0 <= result < MaxNum
Example 1.1: This using gives us a random number between 0(included) and 100(excepted).
1 2 3 4 5 6 7 8 9
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
main(){
srand((unsigned)time(0));
printf("The generated random number: %d",rand() % 100);
getch();
} |
getch() function is used to see the result before the command screen closed and only exist on dos based operating systems. We could use the system(”pause”) function instead of it.
Don’t forget! In the cyber world, there is not random. I think, in the real world is too…
Generating random numbers is very important in programming. We have to use random numbers to show a random record (Image, Saying, Article etc.) on our pages. Also, we can use random numbers for everything that we can imagine. In this article, we will see easiness of generating random numbers with PHP.
Structure 1.0: This is basic statement(void function) and we don’t determine nothing like maximum or minimum limit.
Example 1.0:
Probable Output of Example 1.0:
1.1) Generating a Random Number Between Two Numbers
Structure 1.1: If we want to generate Click to Read Complete Article »
Generating random numbers is very important in Javascript like other programming languages… JavaScript adds dynamism to our html pages. For example: If you want to add a slideshow that is arranged in order at random to your page, you need to generate a random number. In this article, we will see to generate a random number by using Javascript.
Structure 1.0: This is basic statement and we don’t determine nothing like maximum or minimum limit. A float number is generated which falls between 0 and 1.
Example 1.0: Let’s get output.
1
| document.write(Math.random()); |
Probable Output of Example 1.0:
1.1) Generating a Random Number Between Two Numbers
Structure 1.1: If we want to generate Click to Read Complete Article »
Generating random numbers is very important in programming. We have to use random numbers to show a random record (Image, Saying, Article etc.) on our pages. Also, we can use random numbers for everything that we can imagine. In this article, we will see easiness of generating random numbers with ASP.
Structure 1.0:
1 2 3 4
| < %
randomize
response.write int(rnd*MaxNumber)
%> |
Formula: 0 < = result < MaxNumber
At line 2, randomize statement is used to generate a different number for each time we call the rnd() function.
At line 3, a random number was generated between 0 and MaxNumber variable but except the MaxNumber’s value, was converted integer with int() function and was printed. Let’s make an example.
Example 1.0: This usage gives us a random number between 0 and 100 but except 100. Minimum value of generated number can be Click to Read Complete Article »
Recently Typed