0

Is it possible to generate a random integer from a list of numbers or predefined set - that may not be in order.

For example - generate a random number from 1,2,4,5 (no 3 allowed).

Tony Moreira
  • 93
  • 3
  • 18

1 Answers1

2

You can create an array with all your predefined set into it, then you fill an integer index with a random number from 0 to the array's length. That should do the trick :)

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • Thanks for the quick reply. To expand...is it possible to do the same with an array assigned to a list for example... cardsLST.AddAll(Array As Int(1,2,4,5)) ...if not i could go with an array only. IS there documentation/help on pulling/assigning integer index etc. at the b4a site? I couldn't seem to find anything. In other words - for the list I see how to get an index - but not randomly. – Tony Moreira Oct 14 '11 at 11:48
  • I'm not sure of this one but maybe it can do it : cardsLST.AddAll((Array As Int(1,2,4,5))[Math.round(Math.rand()*4)]); – Frederik.L Oct 14 '11 at 12:04
  • I'm not sure that will work - I'm using Basic4Android - still learning - but not seeing some of that syntax as an option in the documentation, etc. – Tony Moreira Oct 14 '11 at 13:39
  • A slightly different syntax for B4A - but creating a predefined set did the trick. TY. – Tony Moreira Oct 23 '11 at 04:30