How to genrate non-repeating random numbers

Hi everyone, i'm trying to write a for loop that genrate a
list of non-repeating random index number for an array list but i
can't seem to get the random numbers non-repeat. I'm a newbie in
Flex 2 so it would be great if anyone could show me what seems to
be the problem in this code:
Thank you very much.
Richie
for (var j:Number = 0; j < tempArray.length; j++)
var randomNum:Number =
Math.round(Math.random()*tempArray.length);
if (j - 1 >= 0)
while (randomNum == randomIndex[j-1].index)
randomNum = Math.round(Math.random()*tempArray.length);
//Push the random number in an array
randomIndex.push({index: randomNum});
Alert.show(randomNum.toString());
}

Dont' worry about it guys, i already got it thanks!

Similar Messages

  • Non repeating random numbers

    Hi,
    Im writing code to generate 10 random numbers and i have code written so that if a number between 1 and 10 is already chosen, it has to look for another number. heres snippets of the code:
    for (int b = 0; b < 2; b++)
    for (int i = 0; i < a.length; i++)
    int test = (int)(Math.random() * 10);
    boolean r = valueExists(test);
    if (r == (true))
    test = (int)(Math.random() * 10);
    else
    a[i] = test;
    boolean valueExists(int value)
    int z = a.length;
    boolean exists = false;
    for (int j = 0; j < z; j++)
    if (a[j] == value)
    exists = true;
    return exists;
    when i run this code, the numbers do repeat. can anyone help?

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Random;
    public class RandomNumbersDemo {
        private List numbers = new ArrayList();
        private Random random = new Random();
        public static void main(String[] args) {
                 RandomNumbersDemo demo = new RandomNumbersDemo();
            demo.init();
            demo.doThatVoodoo();
        public void doThatVoodoo() {
            while (numbers.size() > 0) {
                System.out.println(getRandomInteger());
        public int getRandomInteger() {
            int element = random.nextInt(numbers.size());
            Integer randomInteger = (Integer) numbers.get(element);
            numbers.remove(element);
            return randomInteger.intValue();
        public void init() {
            for (int i = 0; i < 10; i++) {
                numbers.add(new Integer(i));

  • How can you generate Multiple random numbers from 1 to 49?

    I am very new at programming with the iPhone SDK and I need some help with a project I am working on. What I want to do is set up a window with 6 labels and 1 button. When the user clicks the button, the program will populate 6 randomized numbers each ranging from 1 to 49, and then place each of 6 numbers in a label. But each time the program ends and starts again the numbers cannot be the same, and also when the user clicks the button, no label can have the same number twice, so for example. If label 1 had the number 10, the other 5 labels cannot have that number until the button is clicked again. I know how to set up the interface, I just need the code. I would so greatly appreciate someone's help in this matter. I have been trying to do this for days and I cannot figure it out. Possibly someone who knows tons about Objective C programming can help me!
    Thank-you so very much!!

    I see that you're writing a lottery number generator. Perhaps the easiest way to do it is to emulate a real lottery: fill an array (NSMutableArray, probably) with the numbers between 1 and 49, pick one at random, remove that number from the array, and repeat. (You can think of the numbers as being the balls and the array as being the machine that pops them out.)
    One simple way to get the random indices needed is to extract six random bytes from Randomization Services using SecRandomCopyBytes, then loop over them, using the modulo operator (%) to select an index within the size of the array.
    And no, I'm not going to write your code for you. If I was going to do that, I could package and sell the app myself.

  • How do you generate multiple random numbers between 1 and 49

    I am new at mac programming and I want to make an iPhone app with the free SDK. What I would like to try is a random number generator. I imagine it as when the user clicks a button 6 random numbers from 1 to 49 would appear in a label or another control. If someone could please give me the code that would place these randomized numbers in a label I would greatly appreciate it. I know how to link all the controls together, I just need the code. Thank-you to anyone that can help!

    semi-sudo code goes here
    --prepare and array which will have number from 0 to 49
    --loop thru the array like this
    int i=0
    for (i; i<=49; i++) {
    int a=arc4random() % 49] ;
    int b=arc4random() % 49];
    --here exchange the objects so that they are shuffled
    [myArray exchangeObjectAtIndex:a withObjectAtIndex:b];

  • Selecting Non repeatable random records

    HI all,
     I have 20 records in table. I want to get a random single  record from that table.And the main thing is
    if i having 20 records 1st time i'm getting a random record.the upcoming execution result that does not show previous random record .
    That is out of 20 records ,each time i want to select record that does not occur previously.so at 20 executions i have 20 result with non-repeatable.
    If i am confusing you,sorry for this.....pls help me to get a suffled record at each time
    note: i'm used NEWID() that gives random records but it occurs previously some time

    I cannot reproduce this :-)
    with
    [10] as (select 0 as c union all select 0),
    [11] as (select 0 as c from [10] as a, [10] as b),
    [12] as (select 0 as c from [11] as a, [11] as b),
    [13] as (select 0 as c from [12] as a, [12] as b),
    nums as (select top(20) row_number() over(order by c) as n from [13]),
    choice as (select top(1) n from nums order by checksum(newid()))
    select * from choice order by n 
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to count non repeated cells?

    I have an spreadsheet with a lot of email addresses and some of them are repeated. I need to know exactly how many email addresses I have. It's the same thing as categorize but I need a formula.
    I've tried with this excel formula: =SUMPRODUCT(A2:A10"";1;0)/COUNT.IF(A2:A10;A2:A10&""))
    modified to Numbers in these way: =SUMPRODUCT(IF(A2:A10"";1;0)/COUNT.IF(A2:A10;A2:A10&""))
    But in both cases appears that "A2:A10" it can be considered like a unique value.
    Thanks.

    macgquest wrote:
    I have an spreadsheet with a lot of email addresses and some of them are repeated. I need to know exactly how many email addresses I have. It's the same thing as categorize but I need a formula.
    I've tried with this excel formula: =SUMPRODUCT(A2:A10"";1;0)/COUNT.IF(A2:A10;A2:A10&""))
    modified to Numbers in these way: =SUMPRODUCT(IF(A2:A10"";1;0)/COUNT.IF(A2:A10;A2:A10&""))
    In Numbers, comparing a range to a string is not allowed.
    In Numbers, concatenating a range to a string is not allowed.
    Yvan KOENIG (VALLAURIS, France) vendredi 19 février 2010 08:32:01

  • How to put non-repeating field in a repeating frame?

    I have a formula field that creates the header for a list in a repeating frame. I seem to be in a quandary though-if I include the field inside the repeating frame the header gets repeated. If I put it outside the frame I get an error 'Field references Column at a frequency below its group'. If I move the formula column to the parent query it works except in the case where the child query returns no rows-then I get the header with no data.
    So, I either need a way to display the header inside the repeating frame, but only once, or I need a way to suppress the header when the child query returns no rows.

    So, I either need a way to display the header inside the repeating frame, but only once, or I need a way to suppress the header when the child query returns no rows.You can try one idea. Keep the formula column in detail group and create one more summary column as serial number for the detail records so in repeating frame place that field (formula column) in repeating frame and use the PL/SQL code for that field as below...
    IF :serial_no = 1 THEN
      RETURN TRUE;
    ELSE
      RETURN FALSE;
    END IF;Then it should display for the first record only.
    -Ammad

  • How do i save random numbers?

    below is the coding for random numbers
    public class Random {
    public static void main(String[] args) {
    java.util.Random rand = new java.util.Random();
    for (int i = 0; i < 20; i++) {
    System.out.println(rand.nextInt(10));
    }how do i save the random numbers?????

    That was a quick sample. I'm guessing you're not too familiar with java. I expect the error was because you havn't imported the java.io library.
    import java.io.*;
    public class tester {
      public static void main(String args[]) {
        try {
          PrintWriter out = new PrintWriter(new FileWriter("tmp.txt"));
          java.util.Random rand = new java.util.Random();
          for (int i = 0; i < 20; i++) {
            out.println(rand.nextInt(10));
          out.close();
        } catch (IOException ioe) {
          ioe.printStackTrace();
    }This will compile and run. Notice how you have declared your Random class using java.util.Random? You could have imported java.util.* or java.util.Random then used the declaration 'Random rand = .....' instead (like how i've declared the PrintWriter).
    Rob.

  • How do I generate random numbers from a list of numbers without repeating any number

    I am trying to generate a list of random numbers without any repeating numbers.  For example say the list is from 1 to 15, how do I randomly generate a list of numbers using each number only once?

    pb,
    You can build a randomizer by making a 2-column table with 15 rows. In Column A, Fill with the numbers 1 to 15. In column B Fill with RAND(). Then sort on Column B. There will now be a randomized list of the numbers from 1 to 15 in Column A. You can copy this random list and use it in your application.
    Jerry

  • How to generate unique random numbers

    Hi All,
    I am wondering whether there is already a random library or built-in function available in Java to produce some random numbers between certain ranges that are not repetitive. Let's look at a common examples as follows:
    Random diceRoller = new Random();
    for (int i = 0; i < 10; i++) {
      int roll = diceRoller.nextInt(6) + 1;
      System.out.println(roll);
    }My understanding from this approach is that it allows the same number to be repeated over and over again. However, I would like to find out how to continue generating random numbers from remaining ones that haven't been generated earlier.
    Using the above example to illustrate my intention:
    1st random number generated - possibility of 1 - 6 showed up. Say 5 is picked.
    2nd random number generated - possibility of 1, 2, 3, 4, 6 only. Say 2 is picked.
    3rd random number generated - possibility of 1, 3, 4, 6 available. Say 1 is picked.
    4th random number generated - possibility of 3, 4, 6 left. Say 6 is picked.
    5th random number generated - possibility of 3, 4 remains. Say 4 is picked.
    Any assistance would be much appreciated.
    Many thanks,
    Jack

    htran_888 wrote:
    Hi All,
    I am wondering whether there is already a random library or built-in function available in Java to produce some random numbers between certain ranges that are not repetitive. Let's look at a common examples as follows:
    Random diceRoller = new Random();
    for (int i = 0; i < 10; i++) {
    int roll = diceRoller.nextInt(6) + 1;
    System.out.println(roll);
    }My understanding from this approach is that it allows the same number to be repeated over and over again. However, I would like to find out how to continue generating random numbers from remaining ones that haven't been generated earlier.
    Using the above example to illustrate my intention:
    1st random number generated - possibility of 1 - 6 showed up. Say 5 is picked.
    2nd random number generated - possibility of 1, 2, 3, 4, 6 only. Say 2 is picked.
    3rd random number generated - possibility of 1, 3, 4, 6 available. Say 1 is picked.
    4th random number generated - possibility of 3, 4, 6 left. Say 6 is picked.
    5th random number generated - possibility of 3, 4 remains. Say 4 is picked.
    Any assistance would be much appreciated.If it is your school assignment then you have the answer above (List & the lists length).
    (You might want to look at Collections)

  • Stop random numbers repeating HELP!!

    i have been trying to create ten random numbers between 0-99 without them repeating and put them into an object array. this is the code i have been trying but it doesn't seem to work.
    public static void main (String[] args)
         Random generator = new Random();
         Object[] data = new Object[10];
         int marker = 0;
         for (int i = 0; i< data.length; i++)
         int number = Math.abs( generator.nextInt() )%99+1;
         Integer random = new Integer ( number );
         for ( int a = 0; a < data.length; a++ )
                   if ( ( data[a] ) == random )
                   marker = marker + 1;
                   if ( marker == 0 )
                        data[i] = random;
                   else
                        i = i - 1;
                        marker = 0;
              for (int i = 0; i < data.length; i++)
                   System.out.println( data[i] );

    redesign your code, how is that look? :)
    public static void main (String[] args)
      Random generator = new Random();
      Object[] data = new Object[10];
      Set set = HashSet();
      while(set.size() < 10)
        int number = Math.abs( generator.nextInt() )%99+1;
        Integer random = new Integer ( number );
        set.add(random);
      Iterator iter = set.iterator();
      int j = 0;
      while (iter.hasNext())
        data[j++] = iter.next();
      for (int i = 0; i < data.length; i++)
        System.out.println( data[i] );
    }--lichu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to get the desired total of Random numbers ?

    I m in a big problem and want a urgent solution.
    I want to randomise numbers with specified limit and the total of all the randomised numbers should be desirable.
    e.g. if i m generating random number 24 times in a loop then the total of all the gerenrated numbers should be 100 and each randomised generated number should not be greater than 6.
    Please help me out. is there any method from which i can get, desirable total from the gererated random numbers ?
    Please help.
    thanks in advance

    In my usual manner, I'll throw in a couple of ideas and leave others to discard them again :-)
    Pick random numbers. If, at some point, the sum goes over 100, throw away the last number at instead put in the number that will make the sum exactly 100. Then put in 0 until you have 24 numbers. Those 0s won't look random. If it helps, shuffle all the numbers so the 0 come in random places in the sequence. If it's still not random enough, discard the whole idea. If, on the other hand, you still are m numbers short of 24 numbers and the sum goes under 100 - (m * 6), in a similar manner stop using randomness and throw in the numbers needed to hit 100. Shuffle to distribute the 6s.
    Another idea, there are 100 ^ 24 or 10 ^ 48 ways to put 100 balls into 24 buckets. Find out how many ways there are under the constraint that you can put at most 6 balls into a bucket. Enumerate them, pick one at random and generate it. I would say that this should give you a good randomness, but does it also look that way under your requirements?

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

  • Non repeating frame - when/how to use

    Hi,
    How do I use a non repeating frame on it's own.
    I have one query in which I have selected 3 fields.
    In the paper layout, I have placed 3 fields in a non repeating frame.
    When I run the report, I get a REP-1213 error - Filed 'xxx' at frequency below its group.
    When I place the 3 fields ina repeating frame, it works, because the repeating frame property inspector allows me to enter the group and the non repeating frame does not.
    So, my question is, when and how does one use a non repeating frame.
    I need to understand the principle behind it.
    Thank you
    i

    You mainly use regular frames to group objects together or prevent them from being overwritten. Check the help on About Frames.

  • 6 random numbers without repeat

    I'm trying to make a 6 digit random number generator between 1 and 48. When the user click a button 6 random numbers show up in 6 different textFeilds. The problem is that I don't want the same number twice. How can I generate 6 different random numbers without using return and breaking out of the function?
    import flash.events.Event;
    stop();
    btn.addEventListener (MouseEvent.CLICK, random1);
    function random1 (evt:Event) {
              display1.text = "";
              display2.text = "";
              display3.text = "";
              display4.text = "";
              display5.text = "";
              display6.text = "";
              var r1 = Math.floor(Math.random()*(1+48-1))+1;
              var r2 = Math.floor(Math.random()*(1+48-1))+1;
              var r3 = Math.floor(Math.random()*(1+48-1))+1;
              var r4 = Math.floor(Math.random()*(1+48-1))+1;
              var r5 = Math.floor(Math.random()*(1+48-1))+1;
              var r6 = Math.floor(Math.random()*(1+48-1))+1;
              if (r2 == r1 || r3 == r2 || r4 == r3 || r5 == r4 || r6 == r5) {
                        return;
              var liste:Array = new Array();
              liste.push(r1,r2,r3,r4,r5,r6);
              liste.sort(Array.NUMERIC);
              display1.text = String(liste[0]);
              display2.text = String(liste[1]);
              display3.text = String(liste[2]);
              display4.text = String(liste[3]);
              display5.text = String(liste[4]);
              display6.text = String(liste[5]);

    Here's the code for the approach mentioned with your textfields included
    btn.addEventListener (MouseEvent.CLICK, random1);
    function random1 (evt:Event) {
        var nums:Array = new Array();
        // fill the array of numbers
        for(var i:uint=1; i<=48; i++){
            nums.push(i);
        shuffle(nums);
        // grab the first six in the shuffled array
        var liste:Array = nums.splice(0,6);
        // assign the values to the textfields
        for(var j:uint=1; j<=6; j++){
            this["display"+String(j)].text = String(liste[j]);
    function shuffle(a:Array) {
        var p:int;
        var t:*;
        var ivar:int;
        for (ivar = a.length-1; ivar>=0; ivar--) {
            p=Math.floor((ivar+1)*Math.random());
            t = a[ivar];
            a[ivar] = a[p];
            a[p] = t;

Maybe you are looking for