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);

Similar Messages

  • 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.

  • Working with Large Numbers

    Hi there,
    I am currently doing a school assignment and not looking for answers but just a little guidance.
    I am working with large numbers and the modulo operator.
    I might have some numbers such as :
    int n = 221;
    int e = 5;
    int d = 77;
    int message = 84;
    int en = (int) (Math.pow(message, e) % n);
    int dn = (int) (Math.pow(en, d) % n);Would there be a better way to do this kind of calculation. The dn value should come out the same as message. But I always get something different and I think I might be losing something in the fact that an int can only hold smaller values.

    EJP wrote:
    It might make sense in some contexts to have a positive and negative infinity.
    Yes, perhaps that's a better name. Guess I was harking back to old COBOL days :-).(*)
    But the reason these things exist in FP is because the hardware can actually deliver them. That rationale doesn't apply to BIgInteger.Actually, it does. All I'm talking about is a value that compares higher or lower than any other. That could be done either by a special internal sign value (my slight preference) or by simply adding code to compareTo(), equals() and hashCode() methods that takes the two constants into account (as they already do with ZERO and ONE).
    Don't worry, I'm not holding my breath; but I have come across a few situations in which values like that would have been useful.
    Winston
    Edited by: YoungWinston on Mar 22, 2011 9:07 AM
    (*) Actually, '±infinity' tends to suggest a valid arithmetic value, and I wasn't thinking of changing existing BigInteger/BigDecimal maths (except perhaps to throw an exception if either value is involved).

  • 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]);

  • 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?
    }

  • 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 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]++;

  • 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.

  • 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()

  • Has anyone worked with Item Numbers when creating a form?

    I have two Adobe forms, one for an application fee of $20 and one for an orientation fee of $20. I need to be able to distinguish between the two when in PayPal. I was told by PayPal support that I could add a unique item number when setting up an adobe form which I could then filter on in PayPal to see which $20 came from which form. I can't find an Item Number choice anywhere. Can anyone guide me to the right place?

    I have two Adobe forms, one for an application fee of $20 and one for an orientation fee of $20. I need to be able to distinguish between the two when in PayPal. I was told by PayPal support that I could add a unique item number when setting up an adobe form which I could then filter on in PayPal to see which $20 came from which form. I can't find an Item Number choice anywhere. Can anyone guide me to the right place?

  • I can't send iMessage! I try to send it from Russia to USA number and it looks like sent but it didn't came! But it works with other numbers. What I need to do?

    I did it before, when I was in USA. But now I can't! What's wrong?

    TarahCurrie Sep 10, 2014 3:48 PM
    Restore From Time Machine Backup
    Reinstall OS X
    Get Help Online
    Disk Utility
    MacBook Pro, Mac OS X (10.5.8)
    You are running 10.5.8
    Did you try booting off your Install disk holding the c key down.  From here you can get to the drop down menu just a few clicks in (you are not going to install) and run  the Disk Utility>Repair Disk.  See if it will let you do this.

  • I have 1 apple id, 2 5S iPhones with separates numbers and both ring when I get a call on one of the numbers. How do I switch that of ?

    I have 1 Apple ID, 2 5S iPhones (1 private/1 work) with separates numbers and both phones ring when I get a call on one of the numbers. How do I switch that of ?

    I realize that my wife could make her own iTunes account; however, she's been using mine for about 2 years now and this hasn't been an issue.  I guess with the Family Sharing she can have access to all of our music and apps now with her own account. 
    I'll have to see if unchecking our emails resolves this issue.

  • Problem with decimal numbers..

    hi i posted the other day with a problem I had. I thought it was all sorted but I have just realised that there is another problem.
    I have the following code for an applet:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TextFieldExample extends Applet implements ActionListener {
    TextField textInput;
    String text = "";
    public void init() {
    textInput = new TextField (20);
    add(textInput);
    textInput.addActionListener(this);
    public void actionPerformed(ActionEvent e) {
    text = textInput.getText();
    repaint();
    public void paint(Graphics g) {
         int x = Integer.parseInt(text);
         double y = Math.abs(x);
    g.drawString("You wrote " + text,20,500);
         g.drawString("The absolute value is: " + y,20,100);
    I am trying to do the applet so when you type in an angle, it tell you the sine, cosine etc. I can get it to work with whole numbers like 1,10,300, but it won't work when i try to use decimal numbers. Can anyone help?
    Thnk you in advance!
         

    Hi,
    you are using Integer.parseInt() to convert the input, so it will only work if the text is an integer. Use Double.parseDouble() instead.
    Andre

Maybe you are looking for

  • How to export book in CMYK?

    I'm trying to print a book from Aperture to a PDF using the ICC profile for the printing company. This profile is naturally CMYK. In the print dialogue box, simply selecting their profile and clicking "PDF" with our B&W laser printer gives a mess. Do

  • My macbook runs slow but it has lots of free memory

    Basically, it always takes at least 5 mintues to start up and sometimes it even takes 20 minutes. I've checked how much memory I have and I still have about 155 GB free. It's the white macbook and I got it about 2/3 years ago. Please help!

  • Subquery Not Working

    I am trying to match the subquery data with the data in the 1st query and I received an error message that I am missing an expression, I am not sure what to do at this point, assistance would be greatly appreciated. Error: ORA-00936: missing expressi

  • Error in query and delta load

    Hi people , i have a problem. In my society ther's a document that was revocated from r3. Now when i successively load the delta , on bw the document still be active. How i can do? Where is the error? Thanks

  • STS responsible persons and substitutes have disappeared

    Hello STS Experts, one of our users is facing a big problem. For a certain subplan and planing rounds the the responsible persons and substitute have dissappeared 2 days ago. Beside this disturbing situation we also don't know how that could happen?