Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q598 How can I choose a random integer, say, between 0 and 25?

irt.org | Knowledge Base | JavaScript | Random | Q598 [ previous next ]

Q598 How can I choose a random integer, say, between 0 and 25?

Use the Central Randomizer script:

<script>
<!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/JavaScript/randomizer.html

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

// end central randomizer. -->

</script>

To use the Central Randomizer code, ensure it is placed between the <head> and </head> HTML tags. To create a random floating point number use: rnd(), to create a random integer between 0 and 25, use:

rand(26)-1;

For information about random numbers take a look at Random Numbers & Random Events


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.