Random Numbers Using Text

General FreeBASIC programming questions.
Post Reply
IvorU
Posts: 7
Joined: Apr 20, 2018 18:44

Random Numbers Using Text

Post by IvorU »

The idea for this program was inspired by the Lucy spy ring cipher which used additives.
Quote:
"More importantly, the additive was generated not through a keyword, but by selecting lines at random from almanacs of industrial statistics."

This program is used to create random numbers from text - in this case a page from a book.
An array rndNbrs(0,x) will create a sequential key based on the asc values of the letters in the text.
e.g. if the text was "HGHRDMNSNS" (HIGHER DIMENSIONS with vowels removed) then the sequential key would be 3248156970;
D appears alphabetically first then G ...the tenth in order is the final S.
The program removes the vowels then uses all remaining letters as being one word - in this example it
turns out to be a 903 letter word - the length of the output can be tailored to meet the quota needed.
e.g. a page selected, a line selected, a start letter on the selected line.
each random number will appear in most cases the same amount of times as other numbers 1/10th of the total letters used.
to make the output more secure pass it through tableaus for columnar transpositions or as in this case shuffle the array.

Code: Select all

'sample text used to demonstrate idea...

dim as string Quote(1 to 26)={"The view of a three-dimensional body as a section of a four-dimensional",_
"one leads us to the thought that many three-dimensional bodies, which appear",_
"separate for us, may be sections or parts of one four-dimensional body",_
"  A simple example will illustrate this idea. If we imagine a horizontal plane",_
"intersecting the top of a tree in a direction parallel to the earth, then on this",_
"plane the sections of the branches will appear separate and quite unconnected",_
"with one another. And yet in our space, from our point of view, these are",_
"sections of the branches of one tree, together forming one top, fed by one",_
"common root and casting one shadow.",_
"  Or again, another interesting example illustrating the same idea is",_
"given by the theosophical writer, C. W. Leadbeater, in one of his books. If we",_
"touch the surface of a table with our five fingertips of one hand, there will be",_
"then on the surface of the table only five circles, and on this surface it is",_
"impossible to have any idea either of the hand or of the man to whom the",_
"hand belongs. There will be five separate circles on the table's surface. How",_
"from these, is it possible to picture a man, with all the richness of his physical",_
"and psychological life? It is impossible. Our relation to the four-dimensional",_
"world may be exactly the same as the relationship between that consciousness",_
"which sees the five circles on the table and the man. We see only 'fingertips';",_
"that is why the fourth dimension is incomprehensible for us.",_
"   In addition, we know that it is possible to draw an image of a three­",_
"dimensional body on a plane, that it is possible to draw a cube, a polyhedron,",_
"or a sphere. But it will not be a real cube or a real sphere, but only the",_
"projection of a cube or a sphere on a plane. So it may be that we are justified",_
"in thinking that the three-dimensional bodies we see in our space are images,",_
"so to speak, of four-dimensional bodies, incomprehensible for us."}

dim as byte tally(0 to 9),ascNbr,a,lines
dim shared as byte rndNbrs(0 to 1,1 to 910),digit5
dim shared as long count,rndCount,rndElement,aa

dim shared as string L,last5

Declare sub sortLow(rndNbrs()as byte)
Declare sub shuffle(rndNbrs() as byte)

for lines= 1 to 26
        FOR aa = 1 TO LEN(Quote(lines))
        ascNbr = ASC(ucase(MID(Quote(lines), aa, 1)))
        IF ascNbr > 64 AND ascNbr < 91 THEN
            'remove vowels
            IF ascNbr <> 65 AND ascNbr <> 69 AND ascNbr <> 73 AND ascNbr <> 79 AND ascNbr <> 85 THEN
                count += 1
                rndNbrs(0, count) = count MOD 10 'row to hold random nbrs
                rndNbrs(1, count) = ASC(MID(Quote(lines), aa, 1))'row to hold asc values
            END IF
        END IF        
    NEXT aa
    next lines

PRINT "total letters to use= "; count
'sort by asc values low to high
sortLow(rndNbrs())
sleep
rndCount = count
shuffle(rndNbrs())
 
FOR aa = 1 TO rndCount
    tally(rndNbrs(0,aa)) +=1
    PRINT rndNbrs(0, aa);
    if aa mod 500=0 then sleep '10 line of 50
    IF aa MOD 5 = 0 THEN PRINT ; " "; ' make groups of 5 digits
    IF aa MOD 50 = 0 THEN PRINT   'make line of 10 5 digit groups
NEXT aa
print
PRINT "fallout of random numbers:"
FOR a = 0 TO 9
    PRINT a; "="; tally(a); " "
NEXT a
sleep
END

'**********************************************
sub shuffle(rndNbrs() as byte)
'**********************************************
dim As ULongInt rndSeed,newSeed

rndSeed = TIMER 
rndSeed =rndSeed^3 '18+ digit seed
PRINT "rnd nbr seed= "; rndSeed
' FisherYates shuffle
RANDOMIZE rndSeed
WHILE count
    rndElement = INT(RND * count) + 1
    SWAP rndNbrs(0, rndElement), rndNbrs(0, count)
    count = count - 1
    'use last 5 digits as rnd seed
    digit5 = digit5 + 1
    last5 = last5 + STR(rndNbrs(0, rndElement))
    IF digit5 = 5 THEN 'new seed every 5 digits
        newSeed = VAL(last5) ^ 3
        'PRINT "newSeed= "; newSeed;
        last5 = ""
        digit5 = 0
        RANDOMIZE newSeed
    END IF
WEND
end sub

'**********************************************
sub sortLow(rndNbrs()as byte)
'**********************************************
dim as byte Flips
dim as long ki
Flips = 1
WHILE Flips
    Flips = 0
    FOR ki = 1 TO count - 1
        REM sort asc value low to high
        IF rndNbrs(1, ki + 1) < rndNbrs(1, ki) THEN
            SWAP rndNbrs(0, ki + 1), rndNbrs(0, ki)
            SWAP rndNbrs(1, ki + 1), rndNbrs(1, ki)
            Flips = 1
        END IF
    NEXT ki
WEND
end sub

Any constructive observations will be appreciated.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Random Numbers Using Text

Post by lizard »

We could combine your program with mine and make music out of it.

Code: Select all

' color_demo.bas

dim as long rgb_colors(256) = { &h000000, &h0000aa, &h00aa00, &h00aaaa, &haa0000, _
  &haa00aa, &haa5500, &haaaaaa, &h555555, &h5555ff, &h55ff55, &h55ffff, &hff5555, &hff55ff, _
  &hffff55, &hffffff, &h000000, &h141414, &h202020, &h2c2c2c, &h383838, &h444444, &h505050, _
  &h616161, &h717171, &h818181, &h919191, &ha1a1a1, &hb6b6b6, &hcacaca, &he2e2e2, &hffffff, _
  &h0000ff, &h4000ff, &h7d00ff, &hbe00ff, &hff00ff, &hff00be, &hff007d, &hff0040, &hff0000, _
  &hff4000, &hff7d00, &hffbe00, &hffff00, &hbeff00, &h7dff00, &h40ff00, &h00ff00, &h00ff40, _
  &h00ff7d, &h00ffbe, &h00ffff, &h00beff, &h007dff, &h0040ff, &h7d7dff, &h9d7dff, &hbe7dff, _
  &hde7dff, &hff7dff, &hff7dde, &hff7dbe, &hff7d9d, &hff7d7d, &hff9d7d, &hffbe7d, &hffde7d, _
  &hffff7d, &hdeff7d, &hbeff7d, &h9dff7d, &h7dff7d, &h7dff9d, &h7dffbe, &h7dffde, &h7dffff, _
  &h7ddeff, &h7dbeff, &h7d9dff, &hb6b6ff, &hc6b6ff, &hdab6ff, &heab6ff, &hffb6ff, &hffb6ea, _
  &hffb6da, &hffb6c6, &hffb6b6, &hffc6b6, &hffdab6, &hffeab6, &hffffb6, &heaffb6, &hdaffb6, _
  &hc6ffb6, &hb6ffb6, &hb6ffc6, &hb6ffda, &hb6ffea, &hb6ffff, &hb6eaff, &hb6daff, &hb6c6ff, _
  &h000071, &h1c0071, &h380071, &h550071, &h710071, &h710055, &h710038, &h71001c, &h710000, _
  &h711c00, &h713800, &h715500, &h717100, &h557100, &h387100, &h1c7100, &h007100, &h00711c, _
  &h007138, &h007155, &h007171, &h005571, &h003871, &h001c71, &h383871, &h443871, &h553871, _
  &h613871, &h713871, &h713861, &h713855, &h713844, &h713838, &h714438, &h715538, &h716138, _
  &h717138, &h617138, &h557138, &h447138, &h387138, &h387144, &h387155, &h387161, &h387171, _
  &h386171, &h385571, &h384471, &h505071, &h595071, &h615071, &h695071, &h715071, &h715069, _
  &h715061, &h715059, &h715050, &h715950, &h716150, &h716950, &h717150, &h697150, &h617150, _
  &h597150, &h507150, &h507159, &h507161, &h507169, &h507171, &h506971, &h506171, &h505971, _
  &h000040, &h100040, &h200040, &h300040, &h400040, &h400030, &h400020, &h400010, &h400000, _
  &h401000, &h402000, &h403000, &h404000, &h304000, &h204000, &h104000, &h004000, &h004010, _
  &h004020, &h004030, &h004040, &h003040, &h002040, &h001040, &h202040, &h282040, &h302040, _
  &h382040, &h402040, &h402038, &h402030, &h402028, &h402020, &h402820, &h403020, &h403820, _
  &h404020, &h384020, &h304020, &h284020, &h204020, &h204028, &h204030, &h204038, &h204040, _
  &h203840, &h203040, &h202840, &h2c2c40, &h302c40, &h342c40, &h3c2c40, &h402c40, &h402c3c, _
  &h402c34, &h402c30, &h402c2c, &h40302c, &h40342c, &h403c2c, &h40402c, &h3c402c, &h34402c, _
  &h30402c, &h2c402c, &h2c4030, &h2c4034, &h2c403c, &h2c4040, &h2c3c40, &h2c3440, &h2c3040, _
  &h000000, &h000000, &h000000, &h000000, &h000000, &h000000, &h000000, &h000000 }

screenres 834,744,32
windowtitle " 256 Colors Standard Palette"
color  &h000000, &h888888 ' black on gray
cls

screenlock

dim as long i

for x as short = 0 to 7
  for y as short = 0 to 31
    line  (x * 100 + 20, y * 22 + 20) -  step (90, 18), rgb_colors(i),bf
    i += 1
  next y
next x

screenunlock

sleep
Mozart would be deeply impressed.
IvorU
Posts: 7
Joined: Apr 20, 2018 18:44

Re: Random Numbers Using Text

Post by IvorU »

@lizard

Very colourful...

I'm a Berlin fan...you know the guy who only used the black keys on a piano.

Cheers.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Random Numbers Using Text

Post by lizard »

Really? Who was that?
IvorU
Posts: 7
Joined: Apr 20, 2018 18:44

Re: Random Numbers Using Text

Post by IvorU »

@lizard...

“Irving Berlin has no place in American music - he is American music” - Jerome Kern

“The greatest songwriter who has ever lived” - — George Gershwin

Not bad for a man who left school at 13 .
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Random Numbers Using Text

Post by lizard »

Maybe with 14 he would have learned to use the white keys on piano.
Post Reply