Random No.

1. Member
Join Date
Dec 1969
Posts
44

## Random No.

hi,<BR>i am using rs.recordcount to get the total no. of records in a table.<BR><BR>how do i use random fx.<BR><BR>var random = Math.Random()<BR>gives no. b/w 0 & 1<BR><BR>how do i get it to be between 0 & say 12 (total records)<BR><BR>thanks & regards,<BR>abhi malani

2. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

## RE: Random No.

&#060;%<BR>Randomize()<BR>RandomInt = Int((13) * Rnd)<BR>%&#062;

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## That's not JavaScript!

Int??? Rnd??? Randomize???<BR><BR>None of those exist in JavaScript.<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Random No.

You wrote:<BR> var random = Math.Random()<BR> gives no. b/w 0 & 1<BR><BR>Nope, not true. First of all, it is [hl="yellow"]Math.random( )[/hl]<BR><BR>JavaScript *is* case sensitive, you know.<BR><BR>Second of all, the result is a number that is &#062;= 0 and &#060; 1.<BR><BR>That&#039;s important: &#060; 1. *NOT* &#060;= 1.<BR><BR>So if you multiply that result by a count of desired values, you will get a number the is &#062;= 0 and &#060; the count.<BR><BR>Taking your example, you have records numbered from 0 to 12, yes? So you multiply by 13:<BR> var random = 13 * Math.random();<BR>and now random is &#062;= 0 and &#060; 13. <BR><BR>And then you go back to the JS "Math" object and look at the methods it has.<BR>http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjmath.asp<BR><BR>I won&#039;t make you hunt for it: The method you want is "floor".<BR><BR>So, final version:<BR> var random = Math.floor( 13 * Math.random() );<BR>that will give you and *integer* that ranges from 0 to 12.<BR><BR>Just replace the 13 with any other value to get random numbers from 0 to one less than the value.<BR><BR>Hokay?<BR><BR><BR><BR><BR>

5. Member
Join Date
Dec 1969
Posts
44

## RE: Random No.

randomize() is in vb w/o the ()<BR>int is not in jscript & Rnd too.<BR><BR>thanks anyway though.:)

6. Member
Join Date
Dec 1969
Posts
44

## RE: thanks

thanks Mr. B<BR><BR>regards,<BR>abhi malani

7. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

## RE: That's not JavaScript!

Quite a valid VBScript answer though! must have forgot what forum I was in.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•