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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random populate

Status
Not open for further replies.

borsker

Programmer
Jul 24, 2002
147
US
I have a file that i need to populate. For example.
530 of the records need to have '25'
264 of the records need to have '50'
211 of the records need to have '100'

The numbers have to be exact, but they have to be random. i am doing it a rather wierd way, however it does work just slow. any ideas? this is how i am doing it.

for q=1 to 530
use A01_temp
count to nRecordsLeft
store int(1+nRecordsLeft*rand())-1 to nRandom
set filter to recno()=nRandom
if recno()<=0
set filter to recno()=1
endif
replace all Occupy with '25'
erase tempfile.dbf
copy to tempfile
delete all
pack
use A01_long
append from tempfile
next

then i repeat for each value.
 
Hi, this is pretty simple:

Assume you have a table with your 'Occupy' field and another one called 'random' which is a 5 character string:

Code:
use mytable
** create an empty proforma
copy to tempfile stru
select 0
use tempfile
** seed the rand function from the system clock
x = rand(-1)
** throw 530 '25's in
for i = 1 to 530
  append blank
  replace occupy with '25'
  replace random with str(rand()*1000,5,0)
next
** followed by 264 '50's
for i = 1 to 264
  append blank
  replace occupy with '50'
  replace random with str(rand()*1000,5,0)
next
** followed by 211 '100's
for i = 1 to 211
  append blank
  replace occupy with '100'
  replace random with str(rand()*1000,5,0)
next
** now index on the random field
index on random tag random
go top
** then scan the now indexed and random table
** copying the occcupy values (et al) into the original
** one so the 'native' order is now random
do while !eof()
  scatter to tmpfields memo
  select mytable
  append blank
  gather from tmpfields memo
  select tempfile
  skip
enddo
[code]





Regards

Griff
Keep [Smile]ing
 
i think i miss explained. the file called A01_temp is already populated with names and stuff. i need to assign those people values, but only assign a certain amount of values randomly.
 
Ah, I follow.

Well, assuming that you have 530+264+211 records in A01_Temp (otherwise I am way misunderstanding you!), it's not very hard to change:

Code:
use A01_File
** create an empty proforma
copy to tempfile stru
select 0
use tempfile exclusive
alter table tempfile add column random c(5) not null
** seed the rand function from the system clock
x = rand(-1)
** throw 530 '25's in
for i = 1 to 530
  append blank
  replace occupy with '25'
  replace random with str(rand()*1000,5,0)
next
** followed by 264 '50's
for i = 1 to 264
  append blank
  replace occupy with '50'
  replace random with str(rand()*1000,5,0)
next
** followed by 211 '100's
for i = 1 to 211
  append blank
  replace occupy with '100'
  replace random with str(rand()*1000,5,0)
next
** now index on the random field
index on random tag random
go top
** then scan the now indexed and random table
** copying the occcupy values into the original
** one which is in the 'native' order and now has
** a random 'occupy' value which is in quantity specified
select A01_Temp
go top
select tempfile
do while !eof()
  select A01_Temp
  replace  occupy with tempfile.occupy
  skip
  select tempfile
  skip
enddo


Regards

Griff
Keep [Smile]ing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top