Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random numbers
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
KillCloud




PostPosted: Thu Oct 19, 2006 9:54 pm   Post subject: Random numbers

Is there really a way using Math.random() to write a program that generates a random number (double) from 0 (included) to 10(included)?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Oct 19, 2006 10:14 pm   Post subject: (No subject)

Well, Math.random returns a double value from 0.0 to 1.0 (exclusive).

What happens if you multiply that by 10?
KillCloud




PostPosted: Fri Oct 20, 2006 8:29 am   Post subject: (No subject)

ok it will give numbers between 0(inclusive) and 10(exclusive)
but i'm just wondering is there a way to make 10 inclusive
wtd




PostPosted: Fri Oct 20, 2006 9:10 am   Post subject: (No subject)

And if you multiplied by 11?
zylum




PostPosted: Sat Oct 21, 2006 1:31 am   Post subject: (No subject)

multiplying by 11 will obviously not work since he wants a double from 0 to 10 inclusive. multiplying by 11 will give 0 inclusive to 11 exclusive.

KillCloud: for most practical purposes, multiplying by 10 will be sufficient. think about it, what are the odds that you will get one particular number? 1 in the number of possible numbers Math.random() can generate (which is quite a few). so the odds of getting a 10 even if it were inclusive, would be very very slim.
wtd




PostPosted: Sat Oct 21, 2006 12:53 pm   Post subject: (No subject)

Ah... he wants a double. I was going to say, simply floor the resulting value and you get from 0 to 10 inclusive, but that's for integers.
richcash




PostPosted: Sat Oct 21, 2006 3:44 pm   Post subject: (No subject)

You can multiply by 11 and check if it's greater than 10. If it is, then re-randomize.

I'm not good at Java, but I can try :
code:
import java.lang.Math;
public class Randomize {
        public static void main (String [] args) {
                double i = 10.1;
                while (i > 10) {
                        i = Math.random () * 11;
                }
                System.out.println (i);
        }
}
bugzpodder




PostPosted: Sat Oct 21, 2006 9:21 pm   Post subject: (No subject)

abs(Math.random()*20-10)
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sat Oct 21, 2006 10:26 pm   Post subject: (No subject)

Creative. Very Happy
But do note that with this way it is only half as likely to get 10.0 as it is to get any of the other numbers, which makes it not fully random! Wink
bugzpodder




PostPosted: Sun Oct 22, 2006 7:45 pm   Post subject: (No subject)

It depends on what your definition of "fully random" is. Assuming uniform distribution, the probability of getting a 10 on the interval [0, 10] is 0, or in fact the probability of getting a real number is also 0. Also, the computer implements random numbers using a discrete psedorandom number generators.
bugzpodder




PostPosted: Sun Oct 22, 2006 7:46 pm   Post subject: (No subject)

also i've read somewhere that double precisions cannot be used to represent 0 exactly. It is instead represented by something like 10^-304, although the javadoc of Math.random seem to suggest otherwise
richcash




PostPosted: Sun Oct 22, 2006 9:10 pm   Post subject: (No subject)

Java api wrote:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Now, for anyone reading who doesn't know what I'm talking about :
let's call the returned double from Math.random () x
let's call abs (x * 20 - 10) y (because that's what we do to the result)

every possible y value that can be attained has 2 possible x values that it came from. (For instance, x = 0.1 or x = 0.9 -> y = 8)
But y = 10.0 doesn't have 2 x values to come from, it only has x=0.0. x = 1.0 can never happen, Math.random() can't return 1.0.

Therefore, doesn't 10.0 have half the chance of being returned as 8 (or any other double greater than or equal to 0.0 and less than 10.0)?



Now, I know that the chance of any specific double being returned is practically 0 (very, very close to 0) and you should just leave 10 exclusive in the first place as suggested, but it still can't be exactly 0, can it? I guess there's no need to worry about it, I doubt anyone will ever get a 10.0 anyway! Laughing
Aziz




PostPosted: Mon Oct 23, 2006 9:58 am   Post subject: (No subject)

I created a simple randomInteger() method earlier. Hold up *short pause*

Wait, guess I don't. It was something along the lines of what was said earlier.

Simply add one to the upper value. Then, floor the results, for an integer. Hmm. I'll have to look around later.
bugzpodder




PostPosted: Wed Oct 25, 2006 7:01 am   Post subject: (No subject)

richcash wrote:
Java api wrote:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Now, for anyone reading who doesn't know what I'm talking about :
let's call the returned double from Math.random () x
let's call abs (x * 20 - 10) y (because that's what we do to the result)

every possible y value that can be attained has 2 possible x values that it came from. (For instance, x = 0.1 or x = 0.9 -> y = 8)
But y = 10.0 doesn't have 2 x values to come from, it only has x=0.0. x = 1.0 can never happen, Math.random() can't return 1.0.

Therefore, doesn't 10.0 have half the chance of being returned as 8 (or any other double greater than or equal to 0.0 and less than 10.0)?



Now, I know that the chance of any specific double being returned is practically 0 (very, very close to 0) and you should just leave 10 exclusive in the first place as suggested, but it still can't be exactly 0, can it? I guess there's no need to worry about it, I doubt anyone will ever get a 10.0 anyway! Laughing


assuming infinite precision. probability of 0 doesn't mean that you'll never get it. Its like this: pick an integer, any integer. What is the probability of getting 1? The answer is: 0 (duh).

iif you really want to work at it:
Find all bit patterns of doubles which are between 0 and 10.0. Since we are using 32 bit precision, there can be a finitely many of tem. Generate an integer randomly in this interval and pick the corresponding bit pattern.
bugzpodder




PostPosted: Wed Oct 25, 2006 7:03 am   Post subject: (No subject)

richcash wrote:
Java api wrote:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Now, for anyone reading who doesn't know what I'm talking about :
let's call the returned double from Math.random () x
let's call abs (x * 20 - 10) y (because that's what we do to the result)

every possible y value that can be attained has 2 possible x values that it came from. (For instance, x = 0.1 or x = 0.9 -> y = 8)
But y = 10.0 doesn't have 2 x values to come from, it only has x=0.0. x = 1.0 can never happen, Math.random() can't return 1.0.

Therefore, doesn't 10.0 have half the chance of being returned as 8 (or any other double greater than or equal to 0.0 and less than 10.0)?



Now, I know that the chance of any specific double being returned is practically 0 (very, very close to 0) and you should just leave 10 exclusive in the first place as suggested, but it still can't be exactly 0, can it? I guess there's no need to worry about it, I doubt anyone will ever get a 10.0 anyway! Laughing


also you missed the fact that technically, getting a 10 is same as getting a 0
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: