Computer Science Canada

Random numbers

Author:  KillCloud [ 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)?

Author:  wtd [ Thu Oct 19, 2006 10:14 pm ]
Post subject: 

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

What happens if you multiply that by 10?

Author:  KillCloud [ Fri Oct 20, 2006 8:29 am ]
Post 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

Author:  wtd [ Fri Oct 20, 2006 9:10 am ]
Post subject: 

And if you multiplied by 11?

Author:  zylum [ Sat Oct 21, 2006 1:31 am ]
Post 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.

Author:  wtd [ Sat Oct 21, 2006 12:53 pm ]
Post 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.

Author:  richcash [ Sat Oct 21, 2006 3:44 pm ]
Post 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);
        }
}

Author:  bugzpodder [ Sat Oct 21, 2006 9:21 pm ]
Post subject: 

abs(Math.random()*20-10)

Author:  richcash [ Sat Oct 21, 2006 10:26 pm ]
Post 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

Author:  bugzpodder [ Sun Oct 22, 2006 7:45 pm ]
Post 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.

Author:  bugzpodder [ Sun Oct 22, 2006 7:46 pm ]
Post 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

Author:  richcash [ Sun Oct 22, 2006 9:10 pm ]
Post 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

Author:  Aziz [ Mon Oct 23, 2006 9:58 am ]
Post 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.

Author:  bugzpodder [ Wed Oct 25, 2006 7:01 am ]
Post 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.

Author:  bugzpodder [ Wed Oct 25, 2006 7:03 am ]
Post 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

Author:  richcash [ Wed Oct 25, 2006 4:45 pm ]
Post subject: 

I see what you mean about that, but if we had the time to run it 'megagazillions' of times, wouldn't my code produce twice as many 10.0's as your code? This is all abstract and very impractical; for the person implementing this, just exclude 10.0 and multiply by 10 Wink.

Author:  Cheese_slice [ Mon Sep 29, 2008 4:21 pm ]
Post subject:  RE:Random numbers

I'm trying to write a simple program that give out a random number in a range, but it doesn't seem to want to work properly.... (The range is from 20-50)



import java.util.Random;

class randomNum {

public static void main (String [] args) {

Random r = new Random();

int highNum, lowNum;

highNum = 50;

lowNum = 20;

System.out.println(r.nextInt((highNum - lowNum + 1) + lowNum)); }

}

I got my formula from a text book, but it doesn't explain further. (Somewhere the is a logic error, I just can't find it)

Author:  wtd [ Mon Sep 29, 2008 4:40 pm ]
Post subject:  RE:Random numbers

Holy necro post Batman!


: