473,324 Members | 2,268 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,324 software developers and data experts.

help with generating random numbers between -1 and 1

ILtech
6
Math.random() returns a number in the interval [0, 1), right?

So, if I need to change the interval to (-1, 1), would I use do this...

xCoord = Math.random() * 2 - 1;

to change the range to be 2 (so it'd be [0, 2) then subtract one to shift it to (-1, 1)?
Oct 3 '08 #1
3 2142
Laharl
849 Expert 512MB
This oughtta work, give it a shot.
Oct 3 '08 #2
Don't forget that Math.random() returns a double between 0.0 and 1.0. in order to get the correct results you may need a conditional test.

Maybe something like (yes tertiary, my professor would kill me :) )

Expand|Select|Wrap|Line Numbers
  1. int number = Math.random() >= 0.5 ? 1 : -1;
Oct 4 '08 #3
JosAH
11,448 Expert 8TB
Math.random() returns a number in the interval [0, 1), right?

So, if I need to change the interval to (-1, 1), would I use do this...

xCoord = Math.random() * 2 - 1;

to change the range to be 2 (so it'd be [0, 2) then subtract one to shift it to (-1, 1)?
A small nitpick: if you multiply the range [0,1) by two you get the range [0,2);
after subtracting one you get the range [-1,1). Note the inclusion on the left
side and exclusion on the right side.

The solution is simple: while the original random value happens to be zero,
generate another random number.

kind regards,

Jos
Oct 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.