0

I've got a .ini file that contains 10 words (1 word per line)

In my application, I'm assigning the 1st word to something using the following code:

value := somefilename.Strings[0]

The word assigned is thus the word from the 1st line in the .ini file

I'd like, instead of assigning the 1st word, to make it assign a random one from the total 10 located in the .ini file. Cant seem to find a stringlist property for that.

How can I do that?

t1f
  • 3,021
  • 3
  • 31
  • 61
  • Like `Value := SomeFileName.Strings[Random(SomeFileName.Strings.Count - 1)];` (assuming you call `Randomize` somewhere at your application initialization)? – Victoria Mar 10 '18 at 09:24
  • @Victoria Added `initialization Randomize;` (not on the same line, obviously, SO comment formatting shows it on the same line) at the end of my main form. Is that ok? Haven't used this so far. Modified the code to reflect your example, problem is I don't have the Count option, only CountChar after Strings. – t1f Mar 10 '18 at 09:38
  • 1
    Aha, sorry, then do simply `Value := SomeFileName[Random(SomeFileName.Count - 1)];` (assuming `SomeFileName` an instance of type `TStringList`, or `TStrings` descendant). And calling `Randomize` from the module `initialization` section is fine. – Victoria Mar 10 '18 at 09:42
  • 1
    @Victoria Perfect, works like a charm. Thank, appreciate it tremendously. Make it an answer if you wish so I can accept it. Have a good day! – t1f Mar 10 '18 at 09:49
  • @Victoria No. That will never choose the final item. – David Heffernan Mar 10 '18 at 11:32
  • 2
    You're right, my mistake. It should have been `Random(SomeFileName.Count)`. – Victoria Mar 10 '18 at 11:58

1 Answers1

0

As per David and Victoria's comments, this code is ideal.

Value := SomeFileName[Random(SomeFileName.Count)];
t1f
  • 3,021
  • 3
  • 31
  • 61
  • 1
    No. Its wrong I'm afraid. You need `Random(SomeFileName.Count)` – David Heffernan Mar 10 '18 at 11:32
  • 1
    You didn't test it. You didn't test that all possible values could be returned. The last value will never be returned using the code in your answer. Read the documentation. Always always always. Read the documentation. – David Heffernan Mar 10 '18 at 11:49
  • 2
    If the list had just two elements you would not have randomness. Getting this stuff right is invariably important. – David Heffernan Mar 10 '18 at 12:31
  • @Petzy Obviously we have no idea what it is that you're actually implementing. But let's suppose you're randomly selecting prize-winners in a raffle. If I happen to have the last ticket, then: 'Thanks a million.' _or more accurately_: 'Thanks for ***nothing***' – Disillusioned Mar 11 '18 at 01:48
  • @Craig, being last on such list would be part of randomness. Worse would be getting out of index at -1, but so long my mistake was fixed, you can even win ;-) – Victoria Mar 11 '18 at 05:34
  • 2
    My downvote remains, and will do so until you. Rove the offending code. We don't need to see any history of mistakes. You can edit the answer to remove them. For the benefit of any future readers. – David Heffernan Mar 11 '18 at 09:03
  • 1
    I just did say so and assumed nothing – David Heffernan Mar 11 '18 at 12:13