Menu

How to determine if a set of numbers is unique?

Help
2022-06-27
2022-07-04
  • Jack Hoffnung

    Jack Hoffnung - 2022-06-27

    I am generating a set of 16 bytes "randomly" and want to determine if all are unique values. Anyone know of a simple way to do this?

     
  • Anobium

    Anobium - 2022-06-27

    Jack,

    What is the range? and, is Zero a valid random number?

    Create an array with 16 elements,
    Set all 16 elements to zero, (I am assuming zero is not a valid random number)
    Generate a random number check that it does not exist in the array, if unique - put into element 1
    Repeat above put valid result into the next available element that is zero, stopping after the 16th element is not zero.

     
  • Jack Hoffnung

    Jack Hoffnung - 2022-06-27

    Thanks so much; this is the help I needed.

     
  • stan cartwright

    stan cartwright - 2022-06-27

    Do you mean 16 byte numbers that are all different? unique meaning.

    dim nums(16) as byte
    for c1=1 to 16
    nums(c1)=random
    next
    

    gcb would not repeat a number in 16 array. It would be the same numbers every time... from gcb help.
    ;----------------------------------------------
    Random
    Syntax:

    var = Random
    

    Command Availability:

    Available on all microcontrollers

    Explanation:

    The Random function will generate a pseudo-random number between 0 and 255 inclusive.

    The numbers generated by Random will follow the same sequence every time, until Randomize is used.

    Example:

    'Set chip model
    #chip tiny2313, 1
    
    'Use randomize, with the value on PORTD as the seed
    Randomize PORTD
    
    'Generate random numbers, and output on PORTB
    Do
        PORTB = Random
        Wait 1 s
    Loop
    
     
    • Anobium

      Anobium - 2022-06-27

      That is true Stan. For a given RANDOMISE instruction the next 16 RANDOMs would be different.

       
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-04

    Thanks Stan and Anobium; this information will be very helpful with my project!

     
  • stan cartwright

    stan cartwright - 2022-07-04

    @JackHoffnung ... An array of 16 different numbers is a challenge. if you use a port like a-d to use the noise for a truly random value.
    The 16 different numbers 1 to 255 is my latest challenge. It's not as easy as you think but could be done efficiently.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.