Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 855 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

I want to generate random number in JAVASCRIPT?

Thread title: I want to generate random number in JAVASCRIPT?
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
08-20-2008, 11:41 AM
#1
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old  I want to generate random number in JAVASCRIPT?

Hi,
please tell me that how will i generate random number in javascript?

08-20-2008, 11:54 AM
#2
dynamic313 is offline dynamic313
Status: Junior Member
Join date: Jul 2008
Location:
Expertise:
Software:
 
Posts: 52
iTrader: 0 / 50%
 

dynamic313 is on a distinguished road

Send a message via MSN to dynamic313

  Old

something like this should work:
PHP Code:
var randomnumber=Math.floor(Math.random()*101
(the 101 means it will be a number between 0-100 just change 101 for whatever you need)

08-20-2008, 12:18 PM
#3
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old

Originally Posted by dynamic313 View Post
something like this should work:
PHP Code:
var randomnumber=Math.floor(Math.random()*101
(the 101 means it will be a number between 0-100 just change 101 for whatever you need)
Thanks
But i need the javascript code.

08-20-2008, 09:30 PM
#4
xdoomx is offline xdoomx
Status: Member
Join date: Feb 2005
Location: Quebec, Canada
Expertise:
Software:
 
Posts: 441
iTrader: 1 / 100%
 

xdoomx is on a distinguished road

Send a message via AIM to xdoomx Send a message via MSN to xdoomx

  Old

Originally Posted by shilpamg View Post
Thanks
But i need the javascript code.
That is the javascript code.

08-20-2008, 09:36 PM
#5
dynamic313 is offline dynamic313
Status: Junior Member
Join date: Jul 2008
Location:
Expertise:
Software:
 
Posts: 52
iTrader: 0 / 50%
 

dynamic313 is on a distinguished road

Send a message via MSN to dynamic313

  Old

maybe its because i put it in the php tags by mistake, meant to just put it in the normal code tags. sorry if that confussed you shilpamg

08-21-2008, 06:18 AM
#6
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old

Ok,
Thanks

08-21-2008, 06:22 AM
#7
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old

Originally Posted by dynamic313 View Post
maybe its because i put it in the php tags by mistake, meant to just put it in the normal code tags. sorry if that confussed you shilpamg
I have tried this. Its working.
can i display the odd numbers in JAVAScript?

08-25-2008, 05:28 PM
#8
wezy is offline wezy
wezy's Avatar
Status: Junior Member
Join date: Jul 2008
Location: Belgium
Expertise:
Software:
 
Posts: 26
iTrader: 0 / 0%
 

wezy is on a distinguished road

Send a message via MSN to wezy Send a message via Skype™ to wezy

  Old

U could use
Code:
var randomnumber = 2  * Math.floor(Math.random() * 50) + 1
You will get a number between 1 and 99 in this case.

If you want even numbers drop the + 1

Hope this helps.
Wezy

08-28-2008, 11:35 AM
#9
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old

I want to display all the odd numbers.

09-27-2008, 03:49 PM
#10
Zara is offline Zara
Status: Member
Join date: Apr 2006
Location:
Expertise:
Software:
 
Posts: 249
iTrader: 9 / 100%
 

Zara is on a distinguished road

  Old

You can use the Modulus Operator to determine if a number is even or odd. Basically, modulus is an operator that returns the remainder in division.

100 % 2 = 0
99 % 2 = 1
98 % 2 = 0
97 % 2 = 1
ect...

To go from 0 up to your random number, you can throw this method into a loop like so...

Code:
var randomnumber=Math.floor(Math.random()*10) 
for(var i = 0; i <= randomnumber; i++)
{
    if(i % 2)
    {
        alert( i + " is an odd number." );
    } else {
        // alert( i + " is an even number." );
    }
}
Hope this helps.

Closed Thread  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed