Random numbers, not so much to chance

I use a simple piece of code I found online to create a random number between two numbers

new Random().nextInt(6-1)+1);

I am trying to create random numbers for dice.

Everything seems to work fine, but the numbers are not that random. I have a loop configuration to display the code above 50 times and it will make a loop over and over again the same number. Here is an example of the results

1,1,1,1,1,1,4,4,4,4,2,2,2,2,2,1,1,1,1,1

Therefore, it is quite difficult to create a type of yahtzee game when you always roll a yahtzee on the first roll lol.

Is there something I'm missing here?

I agree that the Random function generates only numbers random nickname and there are interesting ways of the algorithm of seeds, I believe that in this case, the problem is that Random is not used correctly.  I think that random is intended to be used as shown in the following fragment.  I also think that random is perfectly satisfactory for use in this case, so I would not personally recommend looking for another algorithm.

Random myGenerator = new Random(System.currentTimeMillis());
String thrown = ""; // Yes, this should be a StringBuffer....
for ( int i = 0; i < 50; i++ ) {
    thrown = thrown + Integer.toString(myGenerator.nextInt(6)+1) + ", ";
}
System.out.println("Thrown: " + thrown);

Here is the result of a few tracks:

Lift: 5, 6, 2, 3, 1, 5, 3, 5, 4, 1, 5, 6, 3, 3, 6, 6, 5, 5, 3, 5, 1, 6, 6, 2, 1, 5, 5, 1, 6, 5, 5, 2, 5, 3, 6, 6, 5, 3, 5, 1, 2, 5, 1, 6, 2, 3, 1, 3, 2, 6,
Lift: 4, 1, 4, 1, 5, 3, 5, 3, 2, 4, 3, 2, 5, 3, 4, 4, 3, 4, 2, 3, 2, 3, 1, 2, 2, 4, 5, 1, 5, 6, 2, 6, 6, 1, 1, 4, 5, 3, 4, 5, 6, 6, 6, 5, 6, 6, 2, 4, 5, 4,

Edit: updated to fix the sample code fragment andgive output.

Tags: BlackBerry Developers

Similar Questions

  • The value of the seed of the generator of random numbers in Matlab node

    Hi all

    I need to use random variables inside a Matlab Labview Commsuite node. I generate with the randn() function. Is it possible to put the seed of the random number generator such as the random numbers are reproducible?

    Hi Steve,.

    You can use randn ('seed', seeds) to initialize the random number generator. The seed must be a positive integer value. For example, randn ('seed', 3). In addition, this seed is global within the entire application. This means that if the randn() function is called MathScript multi-node that could run in parallel, random results are not guaranteed to be the same after you change any part of your code. But you should always get the same results for repeated passages of the same code.

  • How the random numbers will be generated

    The "dice" in LabVIEW function is ised to generate random numbers between 0 and 1. If I create a [100000] array with random numbers between 0 and 100, the appearance of 1 to 100 is the same (about 1000 times each), but the appearance of the 0 is only 500.

    So my question is, on the basis of which will be generated random numbers?

    Mitu salvation,

    It is not a problem of the RNG, it's a problem of your function rounded!

    "To U8" allows you to convert the entire random DBL. ToU8 rounds up to the next integer. So all the number of 0-0.5 will get rounded to 0, but all the numbers from 0.5 to 1.5 will get rounded to 1. If you have twice the range of a number rounded to 1 at the beach of rounding to zero - the same goes for your "end of range" with rounding to 100. 100 should be also less likelihood (in your VI) to appear...

    To get the same probability, you should (explicitly) roundpupils before converting in U8!

    BTW. You can also search the forum to get the same answer by searching for "random"...

  • I'm trying to generate a table of random numbers 30. After each 5 readings a new vi must open and indicate to the user than 5 readings were made. and continue with the generation of the table again.

    because I don't have a sensor now, I am currently generating a table of random numbers 30. After each 5 readings a warning should be given to the user 5 readngs are completed. This cycle must be repeated. the size of the table is 30.

    Please help me, waiting for response as soon as possible.

    Once I have the transducer, I'll take 30 analog samples and then after each 5 smaples this wraning will be displayed din a new VI

    Use a while loop with a delay time representing your sampling interval.

    Use is equal to the count Terminal to see if 4, then 4th iteration = 5th sample.

    Use a box structure. The real deal will only run on the 4th iteration.

    In the case of true place a Subvi with your message of your choice in the front panel. Go to the properties of the VI window and set ' open the front panel when it is called.

    The condition to closing of attention is not given to your description.

    Consider that rather than usign a Subvi to do this, you can use the "dialog box one/two/three button" or "display message" live in the palette "user interface and dialogue."

    Please try it out and send your own VI. Do not provide us with a working solution.

    Kind regards

  • Division of random numbers, see if the result is an integer.

    Simple and yet impossible to find an easy way around it.

    I have 2 RANDOM numbers, I'm dividing them, and I want to see if the result is an integer or not. Basically, I need to know if the divison of random numbers 2 is an integer.

    Who wants KUDOS?

    /SPAW

    Use Quotient and remains, check if the remainder is 0.

  • How can I generate multiple unique random numbers?

    Hello

    I am trying to generate multiple random numbers between a given set of numbers (1-52) and do not have the same number generated twice within this group. I can compare the numbers of last and next with the function to compare, but how would I go about comparison of all numbers generated without using a huge list of shift registers...

    Any help/ideas are welcome and appreciated.

    Jason

    Solved my problem. IM passing the random number through a registry to offset to each case and build a table every time. I then searches the table for the new random number. If the number is not found I get a value of-1, another thing is an index value of 0-everything. If a comparator greater than (-1) indicates that the same number is in the table and then I can raise this matter until the same number is not found.

    Kind regards

    Jason

  • I want just a bunch of random numbers (about 120 of them) from the list, select 'all' and get the computer to rearrange in CNC

    I try both notepad and wordpad. I'm in Winows XP 2003.  I want just a bunch of random numbers (about 120 of them) from the list, select "all" and reorganize in order digital computer. I can't understand how I've done it before. Google says select the balls feature, but all that is put a point in front of a certain number. There is no arrow down to select. No A - Z. No 1,2,3. what Miss me? is the Notepad or wordpad not the place to do that?

    Use Excel.

  • random numbers that appear as you type

    I started to get random numbers, mainly of the 5 and 6 s, appearing during the seizure... anyone have ideas what is the cause and remedy > dave

    There might be problem with your keyboard, if it is the desktop PC, use another keyboard and see if you face the same issue or not.

    Also, in some portable, they have ability to enable some of the alphabet to numberic when you enable specific key, check and see if this feature is enabled.

  • random numbers in a textfield?

    Hi all thx for reading and I hope you can help

    I create a game for children, which shows them how to tell the time

    I need to put two random numbers in a textfield one for hours and one for the minutes

    so that I can get the app to say show me 13:45, for example

    However, I don't want to include the time of 12:00, because that's where the clock will start and defeated the purpose

    the clock is 24 hours, from 1 to 24 hours

    00-60 minutes

    Ive looked everywhere on how to do it but could not find an answer

    at the moment I have a textfield that displays the time as the user adds hours etc that I intend to hide

    while I can tell if this textfield random number is equal to the textfield time then correct display and so on

    How could I go about this?

    any help is appreciated

    Thank you

    fonzio

    Then just replace 0 with 24

    return (hour == 0 ? 24 : hour) + ":" + (minute < 10 ? "0" : "") + minute;
    

    --

    Kenneth Kawamoto

    http://www.materiaprima.co.UK/

  • 3 display unique and random numbers on 6

    The following program gives me the results in the Panel out because I used the trace.  The problem is I want these numbers when I click on a button.  The numbers do not again when the button is clicked, for example not "3, 3, 2" but "3, 2, 5".»  Here is the program that selects random numbers (which, incidentally, are not mine): (I'll give gladly $15 to the animal charity of your choice for the one who can give me a hand with this problem, thank you!)

    var mynumbers:Array is ['1', '2', '3', '4', '5', '6'];.

    var mystuff:Array = [];

    var randomCount:Number = 3;

    var r: Number;

    for (var i = 0; i < randomCount; i ++)
    {

    r = Math.floor (Math.random () * mynumbers.length);

    Mystuff [mystuff. Length] = mynumbers.splice (r, 1);

    }

    trace (mystuff);

    and here is my button code

    generate_btn.addEventListener (MouseEvent.CLICK, coco);

    //3.;
    function coco(event:MouseEvent):void
    {
    }

    Oops, sorry.

    use:

    var mynumbers:Array is ['1', '2', '3', '4', '5', '6'];.

    var randomCount:Number = 3;

    function shuffle(a:Array) {}
    var p:int;
    var t: *;
    var ivar:int;
    for (ivar =. Length-1; Ivar > = 0; Ivar-) {}
    p = Math.Floor ((Ivar+1) * Math.Random ());
    t = a [ivar];
    a [ivar] = a [p];
    a [p] = t;
    }
    }

    and here is my button code

    generate_btn.addEventListener (MouseEvent.CLICK, coco);

    //3.;
    function coco(event:MouseEvent):void
    {

    shuffle(mynumbers);
    

    for (var i: int = 0; i<>

    {if(!this["tf_"+i])}

    This ["tf_" + i] = new TextField();

    }

    This ["tf_" + i] .text = mynumbers [i];

    addChild (this ["tf_" + i]);

    This ["tf_" + i] there = i * 40;

    }

    }

    Thanks for the giveaway.  That's very generous of you.

  • unique random numbers

    How to get the unique random numbers each time in AS3

    I searched but not able to get satisfactory results

    use:

    var tempArray:Array =]

    for (var i: uint = 0; i<>

    tempArray.push (i);

    }

    Shuffle (tempArray);

    You can now browse tempArray for random integers from 0 to moveXml.movie.length () - 1

    function shuffle(a:Array) {}
    var p:int;
    var t: *;
    var ivar:int;
    for (ivar =. Length-1; Ivar > = 0; Ivar-) {}
    p = Math.Floor ((Ivar+1) * Math.Random ());
    t = a [ivar];
    a [ivar] = a [p];
    a [p] = t;
    }
    }

  • Random numbers and generate funds

    Hello.

    I would like to create a very simple equation random addition.

    I have: 3 boxes of dynamic text (r1_txt, r2_txt, a1_txt)

    I want the first two random numbers and 'a1_txt' to display the sum of the first two random numbers.

    Everything is perfect to the point of adding the two numbers and drop the answer in a1_text. What should I do for my last line of code?

    Thanks for your help!

    random numbers
    1.
    function randomNumbers (min:Number, max: Number) {}
    var Results:Number = Math.floor (Math.random () * max) + min;
    return results;
    }

    2.
    new_mc.addEventListener (MouseEvent.CLICK, showRandomnumber);

    3.
    function showRandomnumber(event:MouseEvent):void {}
    r1_txt. Text = randomNumbers (1,100)
    r2_txt. Text = randomNumbers (1,100)
    a1_txt. Text = Number (r1_txt.text) + Number (r2_txt.text);
    }

    Your showRandomnumber() function has a few problems.  TextFields working with channels, so whatever it is assigned to a textfield must be a string value.  You should actually get errors for the first two lines.

    function showRandomnumber(event:MouseEvent):void {}
    r1_txt. Text = String (randomNumbers (1,100));
    r2_txt. Text = String (randomNumbers (1,100));
    a1_txt. Text = String (Number (r1_txt.text) + Number (r2_txt.text));
    }

    If you change which is not fix things, then look to your textfields to the problem

  • Another problem of random numbers

    I say 'Other' because there is an existing thread of random numbers on the first page of this forum, but it is unresponsive to my problem.

    I'm generates names for an array of objects. I have an array of words (strings) to the choice. I use Math.random () * 151 to select indices array of two words. I want to check that the two chosen integers are not the same before assigning the name to the object. Then, I want to check to make sure that none of the two objects have the same name. Simple, right? Here is the code:
        for(int i=0;i<numStars;i++){
            ok=true;
            do{
            int part1=(int)(Math.random()*151);//first index
            int part2=(int)(Math.random()*151);//second index
            if (part1==part2){//make sure they're not the same number
                ok=false;
                System.out.println(part1+" "+part2);
            }else{
                stars.name=new String(starName[part1]+" "+starName[part2]);
    for(int j=0;j<i;j++){
    if(stars[i].name.equals(stars[j].name)) {//make sure no two stars have the same name
    ok=false;
    System.out.println(stars[i].name+" "+stars[j].name);
    }
    }
    }
    }while( ok==false);
    )//end of for loop


    The problem is, if integers part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass. The number doesn't stay the same, but whatever part1 is, part2 is always the same thing. This is only true after matching up one time. It's possible for it to run without ever hitting a duplicate.

    I changed it to ensure that part1 and part2 weren't covering the same range of numbers, and the same error started happening in the lower for loop that compares one object's name to another. This second-level error doesn't occur when part1 and part2 both cover the full range.

    Edited by: 800048 on Oct 11, 2010 3:17 PM

    Edited by: 800048 on Oct 11, 2010 3:25 PM

    800048 wrote:
    The problem is that if whole part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass.

    I don't think it's possible with the code you have posted.

    What will happen, however, given that you set never ok = true once part1 and part2 become unequal, is if they are equal in the first pass through the do / while loop, you will never get out of this loop.

    It is also unnecessary and bad form to use == or! = with the value true or false

    // bad
    x == true
    x != false
    
    // good
    x
    
    // bad
    x != true
    x == false
    
    // good
    !x
    
  • 10 iOS does not well at all. Notes is much harder to change more before. The start screen is so clumsy. How can I get rid of all the widgets of the splash screen?

    10 iOS does not well at all. Notes is much more difficult to edit with touch screen as before. How can I erase my start of all the widgets screen? I might want to go back to iOS earlier.

    "How can I erase my start of all the widgets screen?" is the only question that you asked, so here's the answer:

    Some widgets may not be deleted, but you can move the widgets that you use most on the front screen and keep the ones you don't want a rear screen. Or, you can take the widgets you want and move them to a single folder.

  • Press J, K, L keyboard with showa Numlock numbers not letters

    With Numlock on the Keybord shows numbers not letters on "jkl."

    What's wrong?

    Hello

    Maybe you should disable the light below the F11 key.
    Use the FN + F11 key combination to disable this.

    Then J K L buttons should show letters instead of numbers ;)

Maybe you are looking for

  • More storage

    How do I buy more storage so I can download apps more?

  • Satellite L40 - is it supports WPs on Netgear Wireless N Router?

    Hello I just bought the above to use with my Satellite L40, does anyone know if the laptop wireless adapter supports WPs? Thnx

  • WD My Passport do not mount to Airport Extreme

    OS 10.7.5 WD My Passport format (500 GB): (scope, journaled) Airport extreme version: 7.7.3 My Passport works fine on my MacBook Pro, but when I plug in the AE, it's white light just flashes. (I restarted AE and MacBook, in the same sense) and it doe

  • How do we uninstall KB2467175 safe mode?

    Have a PC running Vista restarts continually.  MS Visual C++ KB2467175 has been installed on the start date of the computer problems.  Want to uninstall C++ mode safe but keep message window Installation Service could not be accessed.

  • Need to task for DiskDiagnosticResolver information

    I was running AutoRuns by Sysinterrnals and clicked on the tab Scheduler.  I saw a red circle with an x in it.  I used the jump to feature and it took tasks me.  I could not find the task to be normal or store display hidden tasks. The entry is \Micr