Need Help with random numbers, please help

Hi guys;
I need your help for a school project. I need to generate a random number from 1.0 to 2.0. How would I do that?
Regards,

http://java.sun.com/j2se/1.3/docs/api/java/util/Random.html#nextFloat()

Similar Messages

  • Need hep with random numbers

    Me and my friends play magic the gathering. There is a mode called planechase. I wont to build a program that simulates this. so it will randomize 82 numbers then store them in the order generated. the game also involves drawing the next card, revealig
    next card, and putting cards on bottom. Any help given will be greatly appreciated. I'm just trying to get an idea of where to start.

    Hello,
    Thank you for your post.
    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses the usage issue of Visual Studio IDE such as
    WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System
    and Visual Studio Editor.
    I am moving your question to the moderator forum ("Where is the forum for..?"). The owner of the forum will direct you to a right
    forum.
    In my opinion, you my consult on
    Visual Studio Language Forums: http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=vslanguages
    Best regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Working with random numbers and probabilities

    Hi again,
    i am working with random numbers at the moment.
    in the first step i do create a random number between 3 and 8 which is stored to a variable.
    set myVAR1 to random number from 3 to 8 as integer
    Lets assume myVAR1 is 5
    Now i want to select 5 numbers out of number pool from 1 to 8. Each number should be pickable only once.
    How would i realize that ?
    I guess i need some kind of pool, array and then select 5 out of this array, but i am not sure about the syntax.
    In best case i would like to add probabilities to those 8 numbers in the pool.
    i.e.
    1 (5%),2(5%),3(20%),4(10%),5(20%),6(10%),7(15%),8(15%)
    any help is heavily appreciated
    best regards
    fidel

    Hello fidel,
    For selecting unique numbers from given pool, try something like this.
    --SCRIPT 1
    (* select unique numbers from pool *)
    set pool to {1, 2, 3, 4, 5, 6, 7, 8} -- number pool
    set n to random number from 3 to 8 -- selection count
    return {n, random(n, pool)}
    on random(n, pool)
    set kk to {}
    repeat with i from 1 to count pool
    set end of kk to i
    end repeat
    set rr to {}
    repeat n times
    set k to kk's some integer
    set kk's item k to false
    set end of rr to pool's item k
    end repeat
    return rr
    end random
    --END OF SCRIPT 1
    For introducing selection weight of each number in the pool, you may try the below.
    --SCRIPT 2
    (* select unique numbers from pool with stochastic weights *)
    set pool to {1, 2, 3, 4, 5, 6, 7, 8} -- number pool
    set weights to {5, 5, 20, 10, 20, 10, 15, 15} -- selection weight
    set n to random number from 3 to 8 -- selection count
    return {n, random(n, pool, weights)}
    on random(n, pool, weights)
    script o
    property ww : weights
    property pp : {}
    property kk : {}
    property rr : {}
    repeat with i from 1 to count pool
    repeat my ww's item i times
    set end of my pp to pool's item i
    end repeat
    end repeat
    repeat with i from 1 to count my pp
    set end of my kk to i
    end repeat
    repeat n times
    set k to my kk's some integer
    set end of my rr to my pp's item k
    set j to 0
    set i to 1
    repeat with w in my ww
    set j to j + w
    if k > j then
    set i to j + 1
    else
    repeat with h from i to j
    set my kk's item h to false
    end repeat
    set i to 1
    exit repeat
    end if
    end repeat
    end repeat
    return my rr's contents
    end script
    tell o to run
    end random
    --END OF SCRIPT 2
    Hope this may help,
    H
    Message was edited by: Hiroto

  • Problem with Purchase Order creation with Random numbers .

    Hi Experts
    Currently i am facing an issue with Bapi BAPI_PO_CREATE1 to create Purchase order with random numbers for example items 1, 3,5.
    Please let me know the settings .
    Thanks in Advance

    Hi Neha,
    A reset of the release strategy only takes place if
    -  the changeability of the release indicator is set to '4' in
       case of a purchase requisition and '4' or '6' in case of
       another purchasing document (purchase order, request for
       quotation, contract, scheduling agreement),
    -  the document is still subject to the previous release
       strategy,
    -  the new TOTAL NET ORDER VALUE is higher than the old one.
    The total net order value is linked to CEKKO-GNETW for a purchase order,
    CEBAN-GSWRT for a purchase requisition item-wise release and CEBAN-GFWRT
    for a purchase requisition overall release.
    If you have maintained a Tolerance for value changes during release
    (V_161S-TLFAE), the release strategy is reset when the value of the
    document is higher than the percentage you have specified and if the
    document is still subject to the previous release strategy.
    Review the SAP note 365604 and 493900 for more
    information about Release strategy.
    Regards,
    Ashwini.

  • Need help with random numbers

    hi i need to generate 2 random numbers from array list . and then take out that two numbers from list.
         String[] plcard = { "AC", "KC", "QC", "JC",
              "10C", "9C", "8C", "7C","6C","5C","4C","3C","2C", "AD", "KD", "QD", "JD",
              "10D", "9D", "8D", "7D","6D","5D","4D","3D","2D", "AS", "KS", "QS", "JS",
              "10S", "9S", "8S", "7S","6S","5S","4S","3S","2S", "AH", "KH", "QH", "JH",
              "10H", "9H", "8H", "7H","6H","5H","4H","3H","2H",};
    this is the list if someone can help me i would appreciate
    thanks in advance

    haha, never noticed the .shuffle(List) method!
    maybe java is getting too convenient (just kidding)?
    i never wrote a card game.
    how would the more programmingly gifted of us do this?
    this is my shot at it for what its worth...
    public class Card{
    public Card(int rank, int suit){
    this.rank = rank;
    this.suit = suit;
    int rank (or String i suppose)
    int suit;
    static final club = 1;
    static final spade = 2;
    static final heart = 3;
    static final diamond = 4;
    public class Shuffler{
    public Shuffler(){
    int NumOfDecks = 1;
    Vector ShuffledDeck;
    // num of decks
    for(int i = 0; i < NumOfDecks){
    // 4 suits
    for(int j = 0; j < 4; j++){
    // 13 ranks
    for(int k = 0; k < 13; k++){
    ShuffledDeck.add(new Card(k, j));
    } // suits
    } // num of decks
    Collections.shuffle(ShuffledDeck);
    // Done?
    }

  • How to fill an array with random numbers

    Can anybody tell me how I can fill an array with thousand random numbers between 0 and 10000?

    import java.util.Random;
    class random
         static int[] bull(int size) {
              int[] numbers = new int[size];
              Random select = new Random();
              for(int i=0;i<numbers.length;i++) {
    harris:
    for(;;) {
    int trelos=select.nextInt(10000);
    numbers=trelos;
                        for(int j=0;j<i;j++) {
    if(trelos==numbers[j])
    continue harris;
    numbers[i]=trelos;
    break;
    return numbers;
    /*This method fills an array with numbers and there is no possibility two numbers to be the
         same*/
    /*The following method is a simpler method,but it is possible two numbers to be the same*/
    static int[] bull2(int size) {
         int[] numbers = new int[size];
    Random select = new Random();
    for(int i=0;i<numbers.length;i++)
              numbers[i]=select.nextInt(9);
              return numbers;
         public static void main(String[] args) {
    int[] nikos = random.bull(10);
    int[] nikos2 = random.bull2(10);
    for(int i=0;i<nikos.length;i++)
    System.out.println(nikos[i]);
    for(int i=0;i<nikos2.length;i++)
    System.out.println(nikos2[i]);

  • Working with random numbers

    is there a way of creating a series of 6 random numbers from 1-9 without repeating a single number? i have currently developed the following method which so far generates a random number excluding zero, but ive no idea of how to ensure that i get different numbers everytime....
    private int randomNumber ()     {
            int num;
            do {
                   num = (int) (Math.random()*10);
            } while (num == 0);
            return num;
    }anyone got any ideas?

    use this insteadRandom rand = new Random();
        // Random integers that range from from 0 to n
        int n = 10;
       int  i = rand.nextInt(n+1);

  • Need some help with Random numbers.

    Hey,
    Im not sure why this is not working....
        public static void main(String[] args) {
        System.out.printf ("Enter n:\n");
        Scanner keyboard = new Scanner (System.in);
            int n1=0,
            n2=0,
            n3=0,
            n4=0,
            n5=0,
            n6=0,
            facevalue,
            x1,
            count = 0;
            double x = keyboard.nextDouble ();
            x1 = rollDie ();
            System.out.println(x1);
        while (count < x) {
            facevalue = 1+x1.nextInt (6);
            if (facevalue == 1) n1++;
            if (facevalue == 2) n2++;
            if (facevalue == 3) n3++;
            if (facevalue == 4) n4++;
            if (facevalue == 5) n5++;
            if (facevalue == 6) n6++;
            count += 1 ; }
        System.out.printf ("%d %d %d %d %d %d\n",n1,n2,n3,n4,n5,n6);
            int maxValue = n1;
            if (maxValue < n2)
                maxValue = n2;
            if (maxValue < n3)
                maxValue = n3;
            if (maxValue < n4)
                maxValue = n4;
            if (maxValue < n5)
                maxValue = n5;
            if (maxValue < n6)
                maxValue = n6;
        System.out.printf ("Max %d\n",maxValue);
        System.out.printf ("Avg %f\n",
        1/x*(n1*1+n2*2+n3*3+n4*4+n5*5+n6*6));
        public static int rollDie (){
            int x1 = (int) Math.random();
            System.out.println(x1);
            return x1;
    }The random number is always 0. Also there is an error at facevalue = 1+x1.nextInt (6); That says 'int cannot be dereferenced.' Please help! thanks!

    Also, this particular assignment seems to be a good time for you to learn about arrays. Look for a tutorial on java arrays. This will reduce your lines of code by 75%.
    What I mean is, you have 6 different variables to hold the total number of times each roll occurred. Instead, you could have a length-6 array:
      //declaring
      int[] tally = new int[6];
      //adding one to the tally
      int nextRoll = random.nextInt(6) + 1;
      tally[nextRoll-1]++;

  • Problem in creation of File with random numbers

    hi all,
    i have written a function which generates random num,ber and stores in a text file.
    public static void main(String[ ] args) {
    int count=0;
    System.out.println("Generate Files");
    Random R=new Random();
    File F= new File("C:/e.cartesian");
    try{
    FileWriter fw=new FileWriter(F);
    int c=0;
    int rand=200;
    int l=0;
    for(int i=0;i<3;i++)
    {    for(int j=0;j<5;j++)
    {   for(int k=0;k<80;k++)
    c+=1;
    double d=R.nextDouble()*100;
    Double D=new Double(d);
    int dtoi=D.intValue();
    float itof=(dtoi/(float)100.0)+R.nextInt(200)+l;
    Float f=new Float(itof);
    fw.write(f.toString()+" ");
    System.out.println(f.toString());
    fw.write("\n");
    count+=1;
    l+=200;
    System.out.println("Count==>"+count);
    }catch(IOException e){}
    Problem is , if i run this code with condition k<79 or some other smaller value than 80 it doesnt save any value in text file.

    If you close the file after writing it works: fw.close();

  • Problem with random numbers

    This is a snippet from a class
    public class Cell {
         private int index;  //index of empire
         private int xCoord;  //X coordinate on the grid
         private int yCoord;  //Y coordinate on the grid
         private double solidarity;  //solidarity value of a cell
         private double power;  //power of a cell
         private Random ran;
         public Cell(int i, int x, int y) {
              ran = new Random();
              index = i;
              xCoord = x;
              yCoord = y;
              solidarity = (double)ran.nextInt(100)/100.0;
    ......I make a couple hundred of these cells when i run the program and at random it seems, every Cell i create ends up having the same exact solidarity value, which really messes up my simulation. It seems to happen once every 5 runs or so. What could be causing this to happen?

    In Java 1.4 Random is initalised with the time.
    Thus if you create many Random objects at the same time, they will generate the same "random" values.
    In Java 5.0 it prevents this behaviour.
    In your case the simplest thing to do is to create one Random and share it.
    Or you could create your own random seed using i,x,y for example. (thus ensure the random seed is different for each cell)

  • Need Advice with Syncing Contacts Please

    Hi,
    I was originally going to post a question asking "How can I transfer my contacts from my iPhone to my iMac Address Book (which is currently empty)?" However, after spending over an hour searching for an answer online, it sounds like there's no easy way to do this.
    I'm new to Syncing and am wondering with all these hoops and hurdles if I am misunderstanding something fundamental about the process. It would seem natural to me that when I connect my iPhone to my iMac, that there would be an easy, even automatic way for all my contacts Sync (in my mind, share a mirror image), regardless of which device they were created on.
    I would greatly appreciate any thoughts. I would also be interested in people's thoughts on the free version of iCloud. For example, with iCloud i guess my phone would sync wirelessly and my iMac would update via my FiOS internet.... ?

    futureperfect wrote:
    Hi,
    I was originally going to post a question asking "How can I transfer my contacts from my iPhone to my iMac Address Book (which is currently empty)?" However, after spending over an hour searching for an answer online, it sounds like there's no easy way to do this.
    Actually, it's pretty simple. Make sure there is at least one entry in the address book on your Mac.
    Plug the phone it and go to the info tab for it in iTunes. Set it up to sync the contacts to the Mac address book. So long as the one on the Mac is NOT empty, you will be able to merge the two.
    Once you do that, changes made on one will be reflected on the other.
    You can't use iCloud on the Mac unless you update to OS X Lion.

  • Random numbers, whithin a button handler class

    For my last project I need to create a program that opens a window with 10 text boxes, all displaying "0" at first and a button labled New Numbers. When this button is pushed it needs to generate random numbers within all of the text fields. I have been able to create one that has the text boxes and the button, but in the button handler class the random numbers will not generate properly.I figured that if I could get the button handler class to at least change just the first text field then I would be able to get the rest, but I can't even seem to get that far. Below is what I have so far, any help or guidence would really be appreciated.. Thanks
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Program6
         public static void main(String [] args)
              MyFrame frame = new MyFrame("Alan Jackson - Program 6");
              frame.pack();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
    class MyFrame extends JFrame
         JTextField [] txt = new JTextField[10];
         JButton btnNewNumbers;
         public MyFrame(String S)
              super(S);
              setLayout(new FlowLayout());
              btnNewNumbers = new JButton("New Numbers");
              int i = 0;
              while(i<10)
                   txt[i] = new JTextField("0",12);
                   add(txt);
                   i++;
              add (btnNewNumbers);     
              btnNewNumbers.addActionListener(new ButtonHandler());
              class ButtonHandler implements ActionListener
                   public void actionPerformed(ActionEvent random)
                        txt[0].setText((int) (Math.random() * 100 +1));                    

    Next time, please use CODE tags, to post your code.
    class MyFrame extends JFrame{
         JTextField [] txt = new JTextField[10];
         JButton btnNewNumbers;
         public MyFrame(String S){
              super(S);
              setLayout(new FlowLayout());
              btnNewNumbers = new JButton("New Numbers");
              int i=0;
              while(i<10){
                   txt[i] = new JTextField("0",12); //<<<<< changed
                   add(txt);
                   i++;
              add (btnNewNumbers);
              btnNewNumbers.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent random){
                        for(int i = 0; i< 10; i++){
                             txt[i].setText(Integer.toString((int) (Math.random() * 100 +1)));

  • How to fill array with random number?

    I need to fill a 3-dimensional array that has user-controlled dimension sizes with random numbers (1-10). I'm unsure of how to do this. I feel like I have to use the initialize array and maybe the build array functions somehow but I'm not entirely sure. Any help would be appreciated. Thanks.

    Something like this
    Kudos are always welcome if you got solution to some extent.
    I need my difficulties because they are necessary to enjoy my success.
    --Ranjeet
    Attachments:
    Array with random number.vi ‏9 KB

  • Alter a column with sequence numbers

    Hi to all,
    i have a table (sample) with cplumns: sno, sname,dob and so on..........
    In this table i have milions of rows with random numbers in column sno
    i want to give the give the sequence numbers from 1 to 10000 in the sno column to the begining rows in the table
    (Oracle)
    could u post the code plese
    thanks & Regards
    Tangella

    Tangella wrote:
    Hi to all,
    i have a table (sample) with cplumns: sno, sname,dob and so on..........
    In this table i have milions of rows with random numbers in column sno
    i want to give the give the sequence numbers from 1 to 10000 in the sno column to the begining rows in the table
    (Oracle)
    could u post the code plese
    thanks & Regards
    TangellaHow do you define "the beginning rows in the table"? Rows in a relational, heap table have no "order" to them. There is no concept of "first".
    And once that is defined, why would you want to assign sequence number to only 10k our of "millions" of rows?

  • I need help with this program ( Calculating Pi using random numbers)

    hi
    please understand that I am not trying to ask anymore to do this hw for me. I am new to java and working on the assignment. below is the specification of this program:
    Calculate PI using Random Numbers
    In geometry the ratio of the circumference of a circle to its diameter is known as �. The value of � can be estimated from an infinite series of the form:
    � / 4 = 1 - (1/3) + (1/5) - (1/7) + (1/9) - (1/11) + ...
    There is another novel approach to calculate �. Imagine that you have a dart board that is 2 units square. It inscribes a circle of unit radius. The center of the circle coincides with the center of the square. Now imagine that you throw darts at that dart board randomly. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. The area of a circle with unit radius is just � square unit. The area of the dart board is 4 square units. The ratio of the area of the circle to the area of the square is � / 4.
    To simuluate the throwing of darts we will use a random number generator. The Math class has a random() method that can be used. This method returns random numbers between 0.0 (inclusive) to 1.0 (exclusive). There is an even better random number generator that is provided the Random class. We will first create a Random object called randomGen. This random number generator needs a seed to get started. We will read the time from the System clock and use that as our seed.
    Random randomGen = new Random ( System.currentTimeMillis() );
    Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.
    A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. There is a method nextDouble() that will return a double between 0.0 (inclusive) and 1.0 (exclusive). But we need random numbers between -1.0 and +1.0. The way we achieve that is:
    double xPos = (randomGen.nextDouble()) * 2 - 1.0;
    double yPos = (randomGen.nextDouble()) * 2 - 1.0;
    To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is Math.sqrt ( xPos * xPos + yPos * yPos ). The radius of the circle is 1 unit.
    The class that you will be writing will be called CalculatePI. It will have the following structure:
    import java.util.*;
    public class CalculatePI
    public static boolean isInside ( double xPos, double yPos )
    public static double computePI ( int numThrows )
    public static void main ( String[] args )
    In your method main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. You will compare your result with the value given by Math.PI. The quantity Difference in the output is your calculated value of PI minus Math.PI. Use the following number of throws to run your experiment - 100, 1000, 10,000, and 100,000. You will call the method computePI() with these numbers as input parameters. Your output will be of the following form:
    Computation of PI using Random Numbers
    Number of throws = 100, Computed PI = ..., Difference = ...
    Number of throws = 1000, Computed PI = ..., Difference = ...
    Number of throws = 10000, Computed PI = ..., Difference = ...
    Number of throws = 100000, Computed PI = ..., Difference = ...
    * Difference = Computed PI - Math.PI
    In the method computePI() you will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will call the method isInside() to determine if the point is inside the circle or not. This you will do as many times as specified by the number of throws. You will keep a count of the number of times a dart landed inside the circle. That figure divided by the total number of throws is the ratio � / 4. The method computePI() will return the computed value of PI.
    and below is what i have so far:
    import java.util.*;
    public class CalculatePI
      public static boolean isInside ( double xPos, double yPos )
         double distance = Math.sqrt( xPos * xPos + yPos * yPos );        
      public static double computePI ( int numThrows )
        Random randomGen = new Random ( System.currentTimeMillis() );
        double xPos = (randomGen.nextDouble()) * 2 - 1.0;
        double yPos = (randomGen.nextDouble()) * 2 - 1.0;
        int hits = 0;
        int darts = 0;
        int i = 0;
        int areaSquare = 4 ;
        while (i <= numThrows)
            if (distance< 1)
                hits = hits + 1;
            if (distance <= areaSquare)
                darts = darts + 1;
            double PI = 4 * ( hits / darts );       
            i = i+1;
      public static void main ( String[] args )
        Scanner sc = new Scanner (System.in);
        System.out.print ("Enter number of throws:");
        int numThrows = sc.nextInt();
        double Difference = PI - Math.PI;
        System.out.println ("Number of throws = " + numThrows + ", Computed PI = " + PI + ", Difference = " + difference );       
    }when I tried to compile it says "cannot find variable 'distance' " in the while loop. but i thought i already declare that variable in the above method. Please give me some ideas to solve this problem and please check my program to see if there is any other mistakes.
    Thanks a lot.

    You've declared a local variable, distance, in the method isInside(). The scope of this variable is limited to the method in which it is declared. There is no declaration for distance in computePI() and that is why the compiler gives you an error.
    I won't check your entire program but I did notice that isInside() is declared to be a boolean method but doesn't return anything, let alone a boolean value. In fact, it doesn't even compute a boolean value.

Maybe you are looking for

  • Hai all

    can anybody tell me why we cannot run ALV GRID display in background. What will happen if i do this. how can we do in background

  • Oracle linux 6

    How to Install Weblogic on oracle linux 6 without GUI? thanks.

  • IOS problem

    i was using my friends apple id and now i cannot use it. i went to apple store and they told me get bill copy. my phone is 2 years old and i don't have it please help me

  • Help need div to expand to height of browser

    Hi is it possible with CSS to get a div to expand to the height of the browser (IE6, IE7 FF etc.). On this design I need to have the 'Nav column' and 'main content area' to end at the bottom of the browser. It works OK when the content in the 'main c

  • Database files to /u02 & flashbackup to /u03,

    OS: AIX 5.2 DB: 10.2.0.1.0 Hello All, I succesfully installed Oracle Database on AIX. We wanted to use Oarcle Database filesystem as follows: /u01 for Oracle /u02 for datafiles /u03 for backup (flashbackup) But we missed Advance configuration method