Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On init of a textbox, insert random numbers and letters

Status
Not open for further replies.

Filip Brnic

Programmer
Dec 25, 2023
43
RS
Hello, i think this is pretty straight forward what im trying to achieve but i think its a little harder, i want a textbot to on init insert random letters and numbers into its value, but to keep the "-Racun" at the end, i tried using chatgpt but the codes he gave me were straight up wrong and long as hell, is it hard to achieve this? if someone can do this for me id appreciate it, its not for anything im just having fun trying to create something in my project and i need the random value generator :)
 
What's the goal, uising it as a random unique id? GUID would be well suited for that. For cryptography? Then there are cyptographic providers for random numbers better suited than generating your own random letters.

Anyway, RAND() gives a random number. There's a problem, though, with pseudo randomness:
Start VFP and do this:
Code:
For i = 1 to 10
   ? Rand()
EndFor i

Start another VFP and do it again, you get the same 10 random numbers.

What that means is any random letters you'd pick by random numbers would also be the same sequence again and again.

A way out of that dilemma exists, just put RAND(-1) somewhere at start, that's enough to randmoize the random number generation. It doesn't change that the random numbers are a computed sequence only pseudo random and there are better ways to generate randomness, if it plays a role.

Well, and how do you get to letters from numbers between 0 and 1? Well, use CHR(65+INT(RAND()*26)) and you get letters from A to Z, because INT(RAND()*26) will be from 0 to 25, never will be 26, as RAND() always is below 1. If you have as many random letters as you want add "-Racun" at the end and that's constant, where's the problem with that?


Chriss
 
You could try something like this:

Code:
lnNum = 10
  && set this to the number or characters required
RAND(-1)
lcResult = ""
lcValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
FOR lnI =1 TO lnNum
  lcChar = SUBSTR(lcValid, INT(RAND() * 36), 1)
  lcResult = lcResult + lcChar
ENDFOR 
this.Value = lcResult + "-Racun"

I haven't tested this thoroughly, but it should give you a start.

It would be possible to combine some of the lines in the code to make the whole thing a bit shorter. But I have deliberately broken it into simple steps to help you understand it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes its for creating unique ids, but i want the same principe, on the init of the textbox, ill see what i can do with what youve provided both :)
 
i tried using chatgpt but the codes he gave me were straight up wrong and long as hell,

You will never develop any programming skills that way. Even if ChatGP turned up some usable code, you will get nowhere by blindly copying it into your program without understanding what it is doing. Despite the marvels of AI, the only way to learn VFP - or any other language - is to study the various command and functions and to work out for yourself how to use them.

(Of course, that doesn't mean that you shouldn't ask for help in this forum when you need it.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Interesting, Substr(somestring,0,1) does not error, but is empty.

nStartPosition<=0 is not documented, but you get the same as in the documented case where nStartPosition is larger than the length of the string: An empty string.

So you could easily generate too short strings.

Chriss
 
By the way, there's an easy way of generating numbers from 0 to x-1 when using MOD, that doesn't require to absolutely know whether RAND() can be 1 in the extreme case or its result is rounded up to 1:

Code:
MOD(Rand()*0x100000000,X)
or
Code:
Rand()*0x100000000%X

Where X is a whole postitve number >1.

Well, and to push the interval [0, x-1] to [1,x], just add 1:
Code:
Rand()*0x100000000%36+1
generates numbers from 1 to 36 without any doubt, because the mod operation generates integer numbers only, always stays in the range 0 to 35, in this case, and thus no matter how high Rand()*0x100000000 is or even whether it would sometimes have decimals, you always stay in the integer range you wish for.

Chriss
 
myself said:
or even whether it would sometimes have decimals

Well, I should have checked: No.

But INT helps here:
Code:
INT(Rand()*0x100000000%36)+1

And if you want to absolutely ensure no outliers, use MIN and MAX on top:
Code:
MIN(MAX(1,INT(Rand()*0x100000000%36)+1),36)

Chriss
 
The OP mentioned letters and numbers, not just numbers.



If you want to get the best response to a question, please check out FAQ184-2483 first.
 
That said, I see from his later post that what he actually wants is to create unique IDs. He didn't mention that at first. If he had, I wouldn't have wasted my time showing how to create a random string. Oh, well. Not the first time, and certainly not the last.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Filip,

we've come to a point of an inactive thread. The question then always is, whether you gave up on this or found your way.
Anyway, as we now know you wanted to create a random unique alphanumeric ID there was a related discussion about whether SYS(2015) is usable as an id in thread184-1830311.

To use an alphanumeric value as a unique id has pitfalls. Randomness can be enough, if the value generated is long enough to avoid collisions (duplicate values) by chance. One point is that this never can be guaranteed by randomness alone, as improbable as it becomes with large ranges of values. But you can define the population of a field with a unique value using such an almost never failing generator by reusing it, if the number it generated turns out to be a duplicate of something already generated beforehand. That means, it's enough to have the primary key checking uniqueness and in case of an error just generate one more random ID value.

This would work fairly well and only lead to rare double calls of the random id generator and even less so triple or more calls necessary. The major ingredient is the range of values being very large, 36 different characters 0-9 and a-z, or 62, if you include A-Z, would give exponents of 62 as the number of different values and that easily becoms larger than astronimical numbers already with 10-20 characters. So the chance of double values is essentially zero. But if you think of the extreme case of flipping a coin only giving 2 results, you can have 2 records, one for head and one for tail and you can flip coins as much as you like if the generated values only are either head or tail, there won't ever be a third random value usable as id of a third record, so the vastness of the possible IDs is important.

If you generate a unique value for a database the only two very well defined ways are a sequence number that's only limited to an upper bound or a GUID, in some systems also called UUID. In the SYS(2015) related thread I already indicated some have their doubts, but I'd say it's mostly misunderstandings. Nevertheless there's always the fix to just throw out a double generated ID when encountering it.

It becomes harder if uniqueness should be guaranteed in a distributed system and not from a centralized database system. And the other case that's always hindering usage of one of these two major alternatives is specifications. If the unique id must conform to a specific format for serial numbers of products, for example, or any other norm. You can then use randomly generated strings, just always check whether they are dupllicates by chance by keeping book of them. This could even be included in the generating code keeping track of generated Ids and only returning a random result that was checked against all previous generated results. But I can give you an example of an infinite loop by recursion here, just repeat this paragraph... The problem is a generator used in a distributed system cannot make that check. Duplicates would only show in merging data from the distributed systems, not earlier.

Chriss
 
Hello,im aware of the thread being innactive, im working on it, i usually read about the things you send me, on other forums and in help of the visual fox pro, so i usually dont test them out right from the jump, but it helped me with getting the idea how to do it, i guess if i just paste the code it will wor regardless.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top