473,440 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

even random number, javascript


How would I generate a even random number each time? I can generate a
random number each time but not an even one? Here is the code I use for
the random number from 1-100.

<script type="text/javascript">
no=math.random()*100
document.write (math.round(no))
</script>
Thanks,
Deb

*** Sent via Developersdex http://www.developersdex.com ***
Aug 30 '05 #1
14 4888
rf
Debbie Lucier
How would I generate a even random number each time? I can generate a
random number each time but not an even one? Here is the code I use for
the random number from 1-100.


Er, generate a random one from 1-50 and multiply it by 2.

Cheers
Richard.
Aug 30 '05 #2
Debbie Lucier wrote:
How would I generate a even random number each time? I can generate a
random number each time but not an even one? Here is the code I use for
the random number from 1-100.

<script type="text/javascript">
no=math.random()*100
document.write (math.round(no))
</script>


rf's answer should do the trick, but what about zero? Should it be
included, or is your range 2-100?
--
Rob
Aug 30 '05 #3
Zif
Debbie Lucier wrote:
How would I generate a even random number each time? I can generate a
random number each time but not an even one? Here is the code I use for
the random number from 1-100.

<script type="text/javascript">
no=math.random()*100
document.write (math.round(no))
</script>
Thanks,
Deb

*** Sent via Developersdex http://www.developersdex.com ***

Math.round() has a bias toward the higher numbers (4.5 goes to 5
always and never to 4), so the starting number has a lower chance of
occurring that all the others.

A better distribution is from truncating the decimal part of numbers
in a range between min and max plus one :
<script type="text/javascript">

function genRandom( min, max ) {
return min + (max-min+1)*Math.random() | 0;
}

document.write( genRandom( 0, 50 )*2 );

</script>

--
Zif
Aug 30 '05 #4
Zif wrote on 30 aug 2005 in comp.lang.javascript:
Math.round() has a bias toward the higher numbers (4.5 goes to 5
always and never to 4),
Unimportant bias, because the chance of a pseudo random number being
exactly 4.5 should be very very small.

Other deviances between real random and pseudo random will have a much
higher effect, though also unimportant in most script applications.
so the starting number has a lower chance of
occurring that all the others.


what do you mean by "starting number"?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #5
Debbie Lucier <no****@glorybound.net> wrote in message news:Qi*****************@news.uswest.net...

How would I generate a even random number each time? I can generate a
random number each time but not an even one? Here is the code I use for
the random number from 1-100.

<script type="text/javascript">
no=math.random()*100
document.write (math.round(no))
</script>


n = (Math.random()*98 +2) & 0xFE ; // 2 - 98 inclusive

n = (Math.random()*100 +2) & 0xFE ; // 2 - 100 inclusive

n = (Math.random()*100) & 0xFE ; // 0 - 98 inclusive

n = (Math.random()*101) & 0xFE ; // 0 - 100 inclusive

--
S.C.
Aug 30 '05 #6
Zif <Zi***@hotmail.com> writes:
Debbie Lucier wrote:
no=math.random()*100
document.write (math.round(no))

Math.round() has a bias toward the higher numbers (4.5 goes to 5
always and never to 4), so the starting number has a lower chance of
occurring that all the others.


That's not the problem with using Math.round and Math.random together.
Since the range [3.5-4.5[ has the same length as [2.5-3.5[, the chance
of hitting either is close to the same.

A more serious problem is that Math.round(Math.random()*100) has 101
different possible outcomes, with 0 and 100 having only half the
chance of the others.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 30 '05 #7
Lasse Reichstein Nielsen wrote on 30 aug 2005 in comp.lang.javascript:
A more serious problem is that Math.round(Math.random()*100) has 101
different possible outcomes, with 0 and 100 having only half the
chance of the others.


You are so right, Lasse.

Let's try:

Math.floor(Math.random()*100)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #8
Evertjan. wrote:
Lasse Reichstein Nielsen wrote:
A more serious problem is that Math.round(Math.random()*100)
has 101 different possible outcomes, with 0 and 100 having
only half the chance of the others.


You are so right, Lasse.

Let's try:

Math.floor(Math.random()*100)


While we are at it, this seems like a good opportunity to use the
truncating side-effect of bitwise operations as the OP wants only even
numbers. Multiplying by two produces even numbers, but so does shifting
left by one bit:-

((Math.random() * 51) << 1) // even integers 0 to 100 (assuming
// a correct random implementation
// with results 0 to <1)

Richard.
Aug 31 '05 #9
Richard Cornford wrote on 31 aug 2005 in comp.lang.javascript:
While we are at it, this seems like a good opportunity to use the
truncating side-effect of bitwise operations as the OP wants only even
numbers. Multiplying by two produces even numbers, but so does shifting
left by one bit:-

((Math.random() * 51) << 1) // even integers 0 to 100 (assuming
// a correct random implementation
// with results 0 to <1)


x = 1.234
alert(x) // 1.234
x = x << 1
alert(x) // 2

Why is the truncation as it is?

x = 0.234
alert(x) // 0.234
x = x << 1
alert(x) // 0 !!!!

Wrong, in the sense of your application!

btw:

x = -1.234
alert(x) // -1.234
x = x << 1
alert(x) // -2

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 31 '05 #10
Evertjan. wrote on 31 aug 2005 in comp.lang.javascript:
x = 0.234
alert(x) // 0.234
x = x << 1
alert(x) // 0 !!!!

Wrong, in the sense of your application!


I seem not to be awake yet.

It is correct as it should be!

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 31 '05 #11
Evertjan. wrote:
Richard Cornford wrote:
While we are at it, this seems like a good opportunity to use
the truncating side-effect of bitwise operations as the OP wants
only even numbers. Multiplying by two produces even numbers,
but so does shifting left by one bit:-

((Math.random() * 51) << 1) // even integers 0 to 100 (assuming
// a correct random implementation
// with results 0 to <1)


x = 1.234
alert(x) // 1.234
x = x << 1
alert(x) // 2

Why is the truncation as it is?


The floating point number is internally converted into a signed 32 bit
integer prior to the actual shift (this happens with all bitwise
operations except - >>> -, which uses an unsigned 32 bit integer). It is
this conversion that has the truncating (towards zero) side-effect, so
the operand for the actual shift is the number one.

Richard.
Aug 31 '05 #12
Richard Cornford wrote on 31 aug 2005 in comp.lang.javascript:
x = 1.234
alert(x) // 1.234
x = x << 1
alert(x) // 2

Why is the truncation as it is?


The floating point number is internally converted into a signed 32 bit
integer prior to the actual shift (this happens with all bitwise
operations except - >>> -, which uses an unsigned 32 bit integer). It is
this conversion that has the truncating (towards zero) side-effect, so
the operand for the actual shift is the number one.


So this is a legal Math.floor equivalent for positive numbers?

x = x << 0

=========

different when negative:

x = -71.234
x = x << 0
alert(x) // -71
x = -71.234
x = Math.floor(x)
alert(x) // -72
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 31 '05 #13
Evertjan. wrote:
Richard Cornford wrote:
x = 1.234
alert(x) // 1.234
x = x << 1
alert(x) // 2

Why is the truncation as it is?
The floating point number is internally converted into a signed 32
bit integer prior to the actual shift (this happens with all bitwise
operations except - >>> -, which uses an unsigned 32 bit integer).
It is this conversion that has the truncating (towards zero)
side-effect, so the operand for the actual shift is the number one.


So this is a legal Math.floor equivalent for positive numbers?


If the positive number can be accommodated in a 32 bit signed integer.
x = x << 0
or:-

x = x | 0;

- or any other bitwise operation that will not alter its operand. Though
OR zero and shift zero have proved the (approximately equal) fastest of
the possibilities (at between 4 and 14 times faster than Math.floor).
=========

different when negative:

x = -71.234
x = x << 0
alert(x) // -71
x = -71.234
x = Math.floor(x)
alert(x) // -72


Yes, the truncating is always towards zero with bitwise operators. But
they are still a good option when you want to quickly acquire integer
values that will be non-negative and restricted in range, such as for
pixel positioning in animation.

Richard.
Aug 31 '05 #14
JRS: In article <df*******************@news.demon.co.uk>, dated Wed, 31
Aug 2005 11:39:54, seen in news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted :

The floating point number is internally converted into a signed 32 bit
integer prior to the actual shift (this happens with all bitwise
operations except - >>> -, which uses an unsigned 32 bit integer). It is
this conversion that has the truncating (towards zero) side-effect, so
the operand for the actual shift is the number one.


The result of X << Y (etc.) where Y is not a small non-negative integer
look interesting, but one should check that actual results and ECMA
agree (for which it is too late tonight) before contemplating actual
use.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 31 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jennie Friesen | last post by:
Hello-- I would like to display a different line of text (32 different messages) on refresh, BUT with no repeats. The script I am currently using is: <script language=javascript...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
4
by: lharby | last post by:
I'm hoping this is very simple. I am currently using a random quote generator on our intranet. I have 17 quotes, when I add in an 18th and change the makeArray number the code seems not to work....
15
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe ...
1
by: sven.daems | last post by:
Hy I want to add a sort of news service to my site. I've a number of messages, wich I want to be shown in a <marquee> tag. I've found a simple scrit that generates an random message (wich I've...
9
by: L33VaNcL33F | last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985). the number always + 22423 that begin from 10016486 and end at 99999985 Example...
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
34
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.