The Artima Developer Community
Sponsored Link

Java Answers Forum
How to generate a random variable ?

4 replies on 1 page. Most recent reply: Mar 31, 2002 4:13 PM by Avin Sinanan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to generate a random variable ? Posted: Mar 31, 2002 3:12 AM
Reply to this message Reply
Advertisement
Hello I was wondering if anyone knows how to gerenerate a raddom variable about a certain number.

For example if I wanted to generate a random number about 10 then the numbers 8, 9, and 11 and 12 would be very very common..well much more common that 1 and 2 and 3 and 20 and so on.

Any ideas or code?

yours Respectfully Avin Sinanan


Master

Posts: 7
Nickname: plut
Registered: Mar, 2002

Re: How to generate a random variable ? Posted: Mar 31, 2002 7:44 AM
Reply to this message Reply
Hello!

I have one idea but it's not to good:
(And I'm not sure I understand your question..)

You may generate a random number and check this number
with numbers allowed! Ex. (pseudo code:)

// if you want to generate a number about 10, where
// only 8,9,11 and 12 is ok!

int i;

while(true)
// Generate number modulo 20
i = generateRandomNumber() % 20;
// We want only positive numbers!
if(i<0)
i = i*(-1);
// Check number, if true break!
if(checkNumber(i))
break;




function checkNumber(i):
// The number is what we want!
if(i==8 || i==9 || i==11 || i==12)
return true;
// Return false, we dont like this number!
return false;

// End pseudo code!

This code generates a positive number between 0 and 20. Then the number is checked! If it is 8,9,11 or 12 we have a winner and the while loop is terminated. This code is not to good because we generate random numbers. Because of that this code may never terminate. We may never generate the numbers 8,9,11 or 12! But cheer up. May experience tells me that as long as you work with small numbers this would work fine!

Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

Re: How to generate a random variable ? Posted: Mar 31, 2002 9:19 AM
Reply to this message Reply
ok no thats not what am looking for. I don't think I explained it properly.

Ok i assume that u know what a Normal distritution is.

There is a like a mean. And the probablity of getting that mean is the highest in the whole distribution. And as numbers stary further and further away from the mean the probablity of it occuring is less.

Anyway thanks for trying.

Any ideas ?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to generate a random variable ? Posted: Mar 31, 2002 12:27 PM
Reply to this message Reply
Use the nextGaussian() method of Random and add your mean.

Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

Re: How to generate a random variable ? Posted: Mar 31, 2002 4:13 PM
Reply to this message Reply
Matt Gerran thank you so very very much. You saved me a bundleof work. Thanks again.

Flat View: This topic has 4 replies on 1 page
Topic: Personal Java and SSL Previous Topic   Next Topic Topic: Beginner's block

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use