×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# random number between limits

## random number between limits

(OP)
hello
anyone now how i can creat a serie of numers at random with limitations?

So as a example.
I will fill in:
lowest numer:1
highsets numer:8
click on a button will give: 15268473
an other click will give   : 68152437
etc, ect
So a random generator but between values i will type in tekst box

tanks

### RE: random number between limits

the Rand(n) function creates a number between 1 and 'n' as the upper limit.  Run each number thru a loop as you create it and simply throw out any number below your lower limit.  Continue to loop till you have as many as you need.

up = upper limit
lo = lower limit
list = string of 8 random digits
r = generated digit

for x = 8 to 1 step -1
r = rand(up)
if r >= lo then
list = list + string(r)
else
x = x + 1
end if
next

### RE: random number between limits

(OP)
hello lealow,
thanks for your quick reply. I have been playing with the code but can not get it to work. I am not the world best programmer yet. Just started 6 months ago.
is this the right way : probebly not because I can not get it to work. evrytime I click on the button I want a new code of x digiits. I have been putting this code in the click event of the button. Anyidea what I am doing wrong

//sle_lo    singellineedit
//sle_high    singellineedit
//st_code    statick texst for showing the code
//cb_run        commandbuttun to run the code

integer r

for x = 8 to 1 step -1
r = rand(sle_high)
if r >= sle_lo then
list = list + string(r)
else
x = x + 1
end if
next

cheers

### RE: random number between limits

I think this is not so difficult.
Try with this:

//...
//Declaring
integer li_lower, li_upper, li_rand
//Assigning values, li_lower > 0, li_lower < li_upper
//i.e.:
li_lower = 32
li_upper = 153
//Getting the random number
li_rand = (li_lower - 1) + Rand (li_upper - li_lower + 1)
//...

Lastly, you can put the last sentence in a loop to obtain the desired amount of random numbers.

### RE: random number between limits

(OP)
hello Zubizar
I am almost there. You have helpt me. It's almost running
I have this code.
At the moemnt I click the button, the script will produce one number in st_code. Perfect sofar.

Only thing I want now: I want the script to produc en string of 8 ramdom nummers instead of one random number
for exaple:
click : 15487632
click:  12658473
etc, etc ,etc
IS that possible ?

//Declaring
integer li_lower, li_upper, li_rand
integer A = 1, B = 1

//Assigning values, li_lower > 0, li_lower < li_upper
//i.e.:
li_lower = 1
li_upper = 8
//Getting the random number
DO UNTIL A > 8
Beep(A)
A = (A + 1) * B

li_rand = (li_lower - 1) + Rand (li_upper - li_lower + 1)

loop

st_code.text = string(li_rand)

### RE: random number between limits

Barkatu ezazu, schothorst, ez nizun ulertu. Eta orain?.

//...
string ls_random_number
long ll_length_random_number, ll

Randomize (0)
ls_random_number = ""
//Get the length of the random number.
//Here is via assignment...
ll_length_random_number = 13    //i.e., the bad number...
FOR ll = 1 TO ll_length_random_number
ls_random_number = ls_random_number + String (Rand (10) - 1)
NEXT
messagebox ("", ls_random_number)
//...

### RE: random number between limits

(OP)
hi zubizar

yes oke, nice...but all the numbers are only allowed one's in de string. Now it will produce for example 141542145 with 3 times a 4. instead of 12345678

do you know what I mean?
cheers

### RE: random number between limits

Hi,

here is the sulution for your problem.

string    ls_result, ls_ch
int        li_rand, li_i
long    ll_count
string    ls_base = "1234567890"

// make the string longer than 255 characters
FOR li_i = 1 TO 5
ls_base += ls_base
NEXT

// Initialize the random generator
Randomize( cpu() )

ll_count = 0
DO WHILE len( ls_result) < 8 AND ll_count < 10
li_rand = rand( 255)

ls_ch = mid( ls_base, li_rand, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
ll_count ++
LOOP

// check for missing positions. Rarely it may be possible that the algorithm may not produce an 8-character string
li_i = 1
DO WHILE len( ls_result) < 8
ls_ch = mid( ls_base, li_i, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
li_i ++
LOOP

RETURN ls_result

hope this helps you

### RE: random number between limits

Sorry . the better way is to do more loops

string    ls_result, ls_ch
int        li_rand, li_i
long    ll_count
string    ls_base = "1234567890"

// make the string longer than 255 characters
FOR li_i = 1 TO 5
ls_base += ls_base
NEXT

// Initialize the random generator
Randomize( cpu() )

ll_count = 0
DO WHILE len( ls_result) < 8 AND ll_count < 100
li_rand = rand( 255)

ls_ch = mid( ls_base, li_rand, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
ll_count ++
LOOP

// check for missing positions. Rarely it may be possible that the algorithm may not produce an 8-character string
li_i = 1
DO WHILE len( ls_result) < 8
ls_ch = mid( ls_base, li_i, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
li_i ++
LOOP

st_1.text = ls_result + " :: " + string( ll_count)

### RE: random number between limits

(OP)
Hello bernds44

Amazing, how do you make this code up.
I just tryed and the last will work almost perfectly.

It is making a string from 8 random numbers.
the only thing that;s not complete write is that al the numbers have to be between 0 and 8. Now sometimes I get a string 1 2 3 4 5 7 8 9  (where 6 is a missing number, and 9 the bad number)

So if I make a string of 10 numbers I want all the numers between 0 and 11 (1 2 3 4 5 6 7 8 9 10) in there
At the moment this is not working

If it will not take to much time of you and if it's possible, is this possible to change ?
tanks a lot
cheers

### RE: random number between limits

(OP)
Hello again

I just changed the string to 8

string    ls_base = "12345678"

And now every time i get a random numers with the numbers 12345678, and all the numers are in. Perfect.
At the moment I dont't have control over the lengt of the string yet.
I want to make a sle_1 field. After putting in 9 the string should be string    ls_base = "123456789". but I think i can manage this my self.

Thanks for all the help.
I will put in the final code soon for future programmers

### RE: random number between limits

HI again,

to have a n-digit number change the string ls_base to n-digits.
eg.:
4 numbers ls_base = "1234"
7 numbers ls_base = "1234567"
8 numbers ls_base = "12345678"
10 numbers ls_base = "1234567890"

Take kare about the last check after the LOOP if ls_result is of the right length that you do not run out of any stringslength with mid() function. This last check is only nececarry for the seldom rare situation the the forst loop does not result in a n-charachter string.

public function string of_make_string (integer ai_numbers);string    ls_result, ls_ch
int        li_rand, li_i
long    ll_count
string    ls_base = "1234567890"

IF ai_numbers > 10 OR ai_numbers < 1 OR IsNull( ai_numbers ) THEN
// ... error handling
RETURN ""
END IF

// make the base string
ls_base = left( ls_base, ai_numbers)

// make the string longer than 255 characters
DO WHILE len( ls_base) < 256
ls_base += ls_base
LOOP

// Initialize the random generator
// this need not to be done here but should always be done minimally once in an applications run
// to guarantee that not in evrey app-runn the same numbers are produced. look at PB-help!!
Randomize( cpu() )

ll_count = 0
DO WHILE len( ls_result) < ai_numbers AND ll_count < 100
li_rand = rand( 255)

ls_ch = mid( ls_base, li_rand, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
ll_count ++
LOOP

// check for missing positions. Rarely it may be possible that the algorithm may not produce an 8-character string
li_i = 1
DO WHILE len( ls_result) < ai_numbers
ls_ch = mid( ls_base, li_i, 1)
IF pos( ls_result, ls_ch) < 1 THEN
ls_result += ls_ch
END IF
li_i ++
LOOP

return ls_result

### RE: random number between limits

Hello, schothorst.

i'm sorry. I see I have'nt understood you from the beginning. I have'nt been able to deduce,
merely looking at yours examples (15487632, 12658473, ...), the restriction "but all the
umbers are only allowed one's in de string"...
Well, only I wish know now is if I've understood you at last.

//...
integer li_from_0, li_limit
string ls_number, ls_digit

//Supongo, ya que no se explicita la restricción, que li_limit es, además de la longitud
//deseada del string ls_number, el "límite superior" (véase abajo) de los dígitos aleatorios
//que lo constituyen...
//li_from_0 must be 0 or 1.
//If 0, the digits will be 1, 2, ..., li_limit     and 1 <= li_limit <=  9
//If 1,  "     "     "  "  0, 1, ..., li_limit - 1 and 1 <= li_limit <= 10

//I think this is the case you wanted initially...
li_from_0 = 0
li_limit = 8

Randomize (CPU ())
ls_number = ""
DO WHILE (Len (ls_number) < li_limit)
ls_digit = String (Rand (li_limit) - li_from_0)
IF (Pos (ls_number, ls_digit) = 0) THEN&
ls_number = ls_number + ls_digit
LOOP
MessageBox ("", ls_number)
//...

First 3 solutions on my PC: 65413827, 47682135, 24316875.

I think, also, it is possible to find an algorithm more efficient...

Only a last question, schothorst: I'm not able to inagine for what demons you want this code...
Cheers.

### RE: random number between limits

(OP)
Hello Zubizar,

Here is the final script that is good enought for me

//declare variable
integer li_from_0, li_limit,li_limit1
string ls_number, ls_digit

//li_from_0 moet 0 of 1 zijn.
//als 0, het getal zal zijn 1, 2, ..., li_limit     en 1 <= li_limit <=  9
//als 1,  "     "     "  "  0, 1, ..., li_limit - 1 and 1 <= li_limit <= 10

//I think this is the case you wanted initially...
li_from_0 = 0
li_limit = integer(ddlb_1.text)
li_limit1 = li_limit + (li_limit - 1)

Randomize (CPU ())
ls_number = ""
DO WHILE (Len (ls_number) < li_limit1)
ls_digit = String (Rand (li_limit) - li_from_0)
IF (Pos (ls_number, ls_digit) = 0) THEN&
ls_number = ls_number + ls_digit + "-"
LOOP
st_code.text = string(ls_number)
//...

We do a lot of trails (research) with differend treatments.
With this code I can randomly assign treatments to differned groups. Pressing the button, wil give a new random code.

cheers

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!