Jump to content

Script to generate unique random numbers between 0..10


Recommended Posts

Though I haven't tested this in game, I think this bit of code should work to randomly generate 3 UNIQUE numbers between 0 and 6.

I don't really do script writing so I'd appreciate any suggestions re: syntax but also re: efficiency. Is there a more elegant way to handle this ?

Background: this is run one time per gameplay.

 

Int place01
Int place02
Int place03
Int maybe


place01 = Utility.RandomInt(0, 6)

maybe = place01

while (maybe == place01)
    maybe = Utility.RandomInt(0, 6)
    If (maybe != place01)
        place02 = maybe
    Endif
endwhile

maybe = place02

while (maybe == place01 || maybe == place02) 
    maybe = Utility.Randomint(0,6)
    If (maybe != place01 && maybe != place02)
        place03 = maybe
    Endif
endwhile

Edited by csbx
Link to comment
Share on other sites

place01 = Utility.RandomInt(0,6)

place02 = place01

while place02 == place01

    place02 = Utility.RandomInt(0,6)

endwhile

place03 = place01

while place03 == place01 || place03 == place02

    place03 = Utility.RandomInt(0,6)

endwhile

Link to comment
Share on other sites

For 3 numbers, this method is fine.  But suppose you need 40 unique randoms in 0..100 range?

Int[] slots = new int[100]
Int[] results = new int[40]

Int idx = 0

while idx < 100
     slots[idx] = 0
     idx += 1
endwhile

Int ridx = 0

while ridx < 40
    idx = Utility.RandomInt(0,99)
    while slots[idx] == 1
        idx += 1
        if idx > 99
            idx = 0
        endif
    endwhile

    slots[idx] = 1
    results[ridx] = idx
    ridx += 1
endwhile

 

Link to comment
Share on other sites

That's awesome - I could not see at all how any of my code was superfluous and yet I count the lines of yours and clearly some of it was !

Great demonstration of clunky code from a beginner and the elegant rendering of a seasoned pro. Thanks !

Thanks also for the more general code--which I'll dig through just to know how to think about such a thing.

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...