NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
Home / News / Webmaster / Tips and Tricks

Tips and Tricks


Random Events in Scripts

A Random number generator in Javascript

By Catalin Bocanu, Web News Editor

8th of October 2007, 14:50 GMT

Adjust text size:


A Javascript Game Based on Random Numbers
Enlarge picture
Random numbers are very useful in programming, because they allow programmers to control and manage various random events. For example, they can be employed in image rotation scripts, random quote generator, games, stochastic simulations, probability calculations and more.

Every programming language contains functions or classes that permit the generation of random numbers. Random number generators are the
subject of statistical error determination, because most of them output pseudo-random numbers. The truly random numbers are hard to obtain by using simple generating techniques. In every situation you must know the type of distribution of random numbers associated with certain events in order to select the corresponding algorithm for the generator. In Javascript, the random() function will generate a random number between 0 and 1, but never 1, every time is called.

If you need to generate other types of random numbers, for example integers situated in a certain range, the function multiplied with the appropriate number will output a value that must be rounded. The functions available in Javascript to round numbers are ceil(), round() and floor(). ceil() function rounds numbers upwards to the nearest integer as compared to floor() function which has a reverse action of rounding numbers.

Due to the difference in rounding number mechanism, the best results in generating uniform distributed random integers are achieved with floor() function. The round() function is not optimal in generating good random integer numbers, because one of the generated numbers (situated in the middle of the interval) will appear twice more often than others.

You can try to analyse sets of random numbers generated using different rounding techniques in order to establish or identify their distribution. The next code is an example of random integer number generator using floor() function for rounding. It will generate numbers between 0 and 7:

CODE
Random Number Generator


function random(n)
{
var x = Math.floor(Math.random()*n)
return x
}
number = random (8)
document.write('the random integer is ' number)


TAGS:

RANDOM NUMBERS | RANDOM EVENTS | STATISTICAL DISTRIBUTION | JAVASCRIPT RANDOM NUMBERS | RANDOM INTEGERS


Rating:
Fair (2.5/5) 6 vote(s) so far    

Read by 1,097 user(s) | Add comment | Link to this article
Subscribe to news | Print article | Send to friend

© Copyright 2001-2008 Softpedia
Contact:

 

 

SEARCH THE NEWS ARCHIVE :




Today's News
| Yesterday's News | News Archive


MORE RELATED ARTICLES:


A Simple Javascript Example

Reduce Spam by Protecting Your Email Address Name

Introduction to Active Server Pages

The Benefits of Code Comments

Free Image Galleries

What You See is What You Get

The World of PHP

User opinions:

No user comments yet.
Be the first to express your opinion using the form below!

Share your opinion:

Your Name:
Your Email Address:
(will not be used for commercial purposes)
Solve this to prove you're not a bot: =
Your review/opinion:

 






SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   ENTER NEWS SITE   |   ENGLISH BOARD   |   ROMANIAN FORUM