Jump to content
Macro Express Forums

Make random variable not repeat the previous X numbers in a row?


Recommended Posts

Anyone know to make a variable not repeat the same number twice in a row? Thanks.

I'm assuming you're using the Set Integer To A Random Value command.

 

My first instinct is to tell you to modify the N variable (converting to Text String). Do this each time a random number is generated, and append each one to a separate text string (be sure to include a space after each number appended). Now use an If Variable Contains command to determine if the number has already been used. If so, you have the Set Integer command try again and again until it selects a value not yet used.

 

There might be a simpler way, but this is what came to mind first.

Link to post
Share on other sites
Anyone know to make a variable not repeat the same number twice in a row? Thanks.

Ok, so I like a challenge. Here are two macros that both do what you want. The first one is very basic, so you can pick it apart easily and see how it's done.

 

The second one is arguably better. It's much more complex, and allows you to select the maximum value of your random generator from a prompt.

 

For both of these macros, I've set the default maximum to 10, and left the default minimum at 0. Mathematically, that leaves 11 possible numbers that could be generated.

 

Please keep in mind, that as you start running out of numbers, the macro may lag as it ignores previous results and tries again. Theoretically, this lag could last forever.

Because of that, the 2nd macro has a built-in "escape". After 100,000 failures, you'll get a pop-up asking you if you want to continue. If you say Yes, it will try again, up to

100,000 more times, before prompting you again.

 

With only 11 numbers to choose from, you'll see that pop up a surprising number of times when you get down to only 1 or 2 numbers left.

 

I tested this macro with 101 possibilities. The 100,000 Failures error began in the early 70s and by 88 was happening very frequently.

 

Anyway, here are the two macros. I'm providing both Scripting Editor and Direct for the simple one. The complex one will only have Direct Editor.

 

Repeat Until %T1% <> %T1%
 Variable Set Integer %N1% with a Random Number
 Variable Modify Integer: Convert %N1% to text string %T1%
 If Variable %T2% does not contain variable %T1%
Variable Modify String: Append "%T1% " to %T2%
Text Box Display: %T1%
 Else
Text Box Display: No Unique Number Generated
 End If
Repeat End

<REP3:08:000002:000001:0001:1:01:T1><IVAR2:01:06:10><NMVAR:05:01:0:0000001:0:0000000><IFVAR2:4:02:8:T1><TMVAR2:07:02:00:000:000:%T1% ><TBOX4:T:1:CenterCenter000278000200:000:%T1%Your randomly generated number is:

%N1%><ELSE><TBOX4:T:1:CenterCenter000278000200:000:No Unique Number GeneratedNumbers generated so far:

%T2%><ENDIF><ENDREP>

 

Complex Macro:

<IVAR2:02:01:10><IVAR2:02:02:FWhat is the maximum value?FFCenter:Center><TVAR2:04:01:all><REP3:08:000002:000001:0001:1:01:T1><NMVAR:01:06:1:0000002:2:0000001><IVAR2:01:06:%N2%><NMVAR:05:01:0:0000001:0:0000000><IFVAR2:4:02:8:T1><TMVAR2:07:02:00:000:000:%T1% ><TBOX4:T:1:CenterCenter000278000200:000:%T1%Your randomly generated number is:

%N1%><IVAR2:07:01:0><ELSE><TMVAR2:09:03:02:000:000:><TMVAR2:21:03:01:000:000: ><IVAR2:03:12:3><IVAR2:04:12:2><NMVAR:02:03:1:0000004:1:0000003><REP3:01:000001:000001:10000:1:05:><IFVAR2:2:07:1:100000><IFMESS3:00000:1:1:%N7% FailuresYou've generated %N3% of %N6% possibilities.  Further unique selections from this sample may be delayed indefinitely.

Would you like to continue?000529:000433><IVAR2:07:01:0><ELSE><IVAR2:07:01:%N3%><NMVAR:01:03:1:0000002:1:0000002><TVAR2:04:01:%N7%><BREAK><ENDIF><ENDIF><IFVAR2:2:05:1:10000><NMVAR:01:07:1:0000005:1:0000007><ENDIF><ENDREP><IFVAR2:5:03:4:N2><BREAK><ENDIF><ENDIF><ENDREP><TBOX4:T:1:001781Center000344000200:000:That Should Be ItYou've generated %T4% of the numbers between 0 and %N2%

%T2%>

Link to post
Share on other sites

Well after all that work, I just realized you don't mind if numbers repeat. You just don't want them to repeat twice in a row. Whoops!

 

Still, based on the macros I built, it should be simple enough to figure out how to make basic comparisons to make sure two numbers aren't

provided twice in a row. Should also minimize lag :) Sorry about that!

 

EDIT

This is closer to what you wanted, I believe.

 

Variable Set Integer %N1% to 10
Variable Set Integer %N1% from Prompt
Repeat with Variable using %N1%
 Variable Set Integer %N1% with a Random Number
 Variable Modify Integer: Convert %N1% to text string %T1%
 If Variable %T2% <> variable %T1%
Variable Modify String: Copy %T1% to %T2%
Variable Modify String: Append "%T1% " to %T3%
Text Box Display: %T1%
 Else
 End If
Repeat End
Text Box Display: All Generated Numbers

<IVAR2:01:01:10><IVAR2:01:02:FHow many numbers do you need generated?FFCenter:Center><REP3:05:000001:000001:0001:0:01:><IVAR2:01:06:10><NMVAR:05:01:0:0000001:0:0000000><IFVAR2:4:02:2:T1><TMVAR2:09:02:01:000:000:><TMVAR2:07:03:00:000:000:%T1% ><DIS:<TBOX4:T:1:CenterCenter000278000200:000:%T1%Your randomly generated number is:

%N1%><ELSE><ENDIF><ENDREP><TBOX4:T:1:CenterCenter000278000200:000:All Generated Numbers%T3%>

Link to post
Share on other sites

yes the 2nd code is more what im looking for. but how can i make the variable stay internal to the macro without showing or prompting for the text boxes?

 

basically, i just want the variable to generate a random number and then end. but if it generates the same number as the previous macro run, i want it to generate a new one until it's different, before continuing...

Link to post
Share on other sites
yes the 2nd code is more what im looking for. but how can i make the variable stay internal to the macro without showing or prompting for the text boxes?

 

basically, i just want the variable to generate a random number and then end. but if it generates the same number as the previous macro run, i want it to generate a new one until it's different, before continuing...

You can delete or disable the command lines you don't want (i.e. the Text Box Display commands).

 

If I understand you correctly, you want the following:

  • You run the macro.
  • The macro generates a random number.
  • The macro does something with that number.
  • The macro ends.

  • You run the macro again.
  • The macro generates a new number which does not match the previous number.
  • The macro does the same thing it did the last time you ran the macro.
  • The macro ends.

 

When I get a few minutes today, I'll tweak my macro above to do just this. In the mean time, I recommend you take my macros and play with them a bit. Tweak them on your own, and get a feel for how they work. It's really the best way to learn how to build your own.

 

I'll be back in a little while with an example of how to do what I've understood you to need.

Link to post
Share on other sites

yes, you understand me correctly. it would be great to see how it can be done. :)

 

BTW: it seems macro express' random integer generator might have a slight bug, and generates the same number twice in a row too often. even if i set the number range higher, the frequency of the problem occuring still seems the same.

Link to post
Share on other sites
yes, you understand me correctly. it would be great to see how it can be done. :)
I have what I believe to be a working model for your macro.
BTW: it seems macro express' random integer generator might have a slight bug, and generates the same number twice in a row too often. even if i set the number range higher, the frequency of the problem occuring still seems the same.

That is weird. I haven't noticed that, myself, but I don't work with randoms very often. My wife is a highschool Mathematics and Statistics teacher, and she

has told me that not even computers have the capacity to generate completely random sequences. An underlying pattern always emerges.

 

Of course, I argue that true randomness will always generate the appearance of a pattern. They say if you give a monkey a typewriter and infinite time, and he

will eventually produce the complete works of Shakespear - I think that's false, because the monkey will likely develop a preference for a specific key. However,

if you replace the monkey with a computer typing randomly for infinite time, you will end up with the complete works of Shakespear (and everything else ever

written - or that ever will be written - by man... and probably some really good recipes, too).

 

With that said, if you think there is a definite problem with the randomness of the program, I'd suggest contacting ISS support directly.

 

Now, on to the fun.

 

This macro is a bit longer than I had expected, but still very reasonable. The prompts at the beginning can be removed, as long as you know you will always use

the same minimum and maximum numbers. Just set N4 to you standard minimum and N3 to your standard maximum and remove the prompts.

 

// %N1% Random Number
// %N2% Previously Generated Random Number
// %N3% Maximum Value of Randomly Generated Number
// %N4% Minimum Value of Randomly Generated Number

// Note:  The Random Number Generator has a minimum default of 0 wich cannot be changed.
// __Because of this, we have to manipulate the values of %N1% and %N3%
// __I will set these manipulations apart in the body of the text with surrounding  *** Remarks
// __When N1 is generated it could = 0.  If the min is set at 1, then N1 must be increased 
// __by the value of N4 (in this case, 1) in order to ensure N1 >= 1.
// __If the max (N3) is 10, then increasing N1, could result in N1 > N3 (which we must avoid.).  
// __So we start by reducing N3 by the value of N4 to ensure that when N1 is increased, 
// __N1 does not exceed N3.

// %T1% Text Variable Used to Facilitate New and Old Number Comparisons
// %HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.
// This may be different for every computer, so I used these defaults because they should work for anybody.
Variable Set Integer %N4% to 1
Variable Set Integer %N4% from Prompt
Variable Set Integer %N3% to 10
Variable Set Integer %N3% from Prompt
// ***
Variable Modify Integer: %N3% = %N3% - %N4%
// ***
Repeat Until %T1% <> %T1%
 If File Exists "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Set String %T1% from File: "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Modify String: Convert %T1% to integer %N2%
 Else
Variable Set Integer %N2% to %N3%
Variable Modify Integer: Inc (%N2%)
 End If
 Variable Set Integer %N1% with a Random Number
 // ***
 Variable Modify Integer: %N1% = %N1% + %N4%
 // ***
 If Variable %N1% <> variable %N2%
Variable Modify Integer: Convert %N1% to text string %T1%
Variable Modify String: Save %T1% to Text File
Break
 End If
Repeat End
Text Box Display: This Text Box Represents The Rest of Your Macro

<REM2:%N1% Random Number><REM2:%N2% Previously Generated Random Number><REM2:%N3% Maximum Value of Randomly Generated Number><REM2:%N4% Minimum Value of Randomly Generated Number><REM2:><REM2:Note:  The Random Number Generator has a minimum default of 0 wich cannot be changed.><REM2:__Because of this, we have to manipulate the values of %N1% and %N3%><REM2:__I will set these manipulations apart in the body of the text with surrounding  *** Remarks><REM2:__When N1 is generated it could = 0.  If the min is set at 1, then N1 must be increased ><REM2:__by the value of N4 (in this case, 1) in order to ensure N1 >= 1.><REM2:__If the max (N3) is 10, then increasing N1, could result in N1 > N3 (which we must avoid.).  ><REM2:__So we start by reducing N3 by the value of N4 to ensure that when N1 is increased, ><REM2:__N1 does not exceed N3.><REM2:><REM2:%T1% Text Variable Used to Facilitate New and Old Number Comparisons><REM2:%HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.><REM2:This may be different for every computer, so I used these defaults because they should work for anybody.><IVAR2:04:01:1><IVAR2:04:02:FWhat is the minimum random value?FFCenter:Center><IVAR2:03:01:10><IVAR2:03:02:FWhat is the maximum random value?FFCenter:Center><REM2:***><NMVAR:02:03:1:0000003:1:0000004><REM2:***><REP3:08:000002:000001:0001:1:01:T1><IFOTH:01:2:%HOMEDRIVE%%HOMEPATH%/Random Number><TVAR2:01:04:%HOMEDRIVE%%HOMEPATH%/Random NumberT><TMVAR2:05:01:02:000:000:><ELSE><IVAR2:02:01:%N3%><NMVAR:08:02:0:0000001:0:0000000><ENDIF><IVAR2:01:06:%N3%><REM2:***><NMVAR:01:01:1:0000001:1:0000004><REM2:***><IFVAR2:5:01:2:N2><NMVAR:05:01:0:0000001:0:0000000><TMVAR2:17:01:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><BREAK><ENDIF><ENDREP><TBOX4:T:1:CenterCenter000278000200:000:This Text Box Represents The Rest of Your MacroCurrent Number: %N1%>

Link to post
Share on other sites

Thanks! But what is the default folder of C: drive? Does running the macro leave a file behind?

 

BTW: These are the commands to be removed if I want to remove prompts, etc. right?

 

Variable Set Integer %N4% from Prompt

Variable Set Integer %N3% from Prompt

Text Box Display: This Text Box Represents The Rest of Your Macro

 

Also, is it possible to have a lighter version of the macro without the non-zero correction? So that the minimum is always 0 but the max can be changed.

Link to post
Share on other sites
Thanks! But what is the default folder of C: drive? Does running the macro leave a file behind?

 

BTW: These are the commands to be removed if I want to remove prompts, etc. right?

 

Variable Set Integer %N4% from Prompt

Variable Set Integer %N3% from Prompt

Text Box Display: This Text Box Represents The Rest of Your Macro

 

Also, is it possible to have a lighter version of the macro without the non-zero correction? So that the minimum is always 0 but the max can be changed.

Here is the Light Version. You can still choose what the Max will be (though I've removed the prompt, so you'll need to establish the maximum

within the Variable Set Integer %N3% command).

 

I don't know what your default folder is. It depends on your user profile, I believe. On my home computer %HOMEDRIVE%%HOMEPATH% is the "Steven" folder

on my desktop; at work it is simply H:/ . It will be something different on your computer. You can figure out exactly where by building a simple macro like this one:

 

(this is the Direct Editor text... that's how simple this macro is)

<LAUNCHDEL2:0:01%HOMEDRIVE%%HOMEPATH%>

 

This macro (the one that makes sure the random number does not repeat) does leave a file behind (it has to in order to make certain the comparisons

are consistent). You can delete the file manually, or you can build a second macro with the following code to find it and delete it:

 

Again, this is just Direct Editor script:

<DOFILE:08:NN:%HOMEDRIVE%%HOMEPATH%/Random Number>>

 

And now, what we've all been waiting for, the Light Version:

// %N1% Random Number
// %N2% Previously Generated Random Number
// %N3% Maximum Value of Randomly Generated Number
// %T1% Text Variable Used to Facilitate New and Old Number Comparisons
// %HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.
// This may be different for every computer, so I used these defaults because they should work for anybody.
Variable Set Integer %N3% to 10
Repeat Until %T1% <> %T1%
 If File Exists "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Set String %T1% from File: "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Modify String: Convert %T1% to integer %N2%
 Else
Variable Set Integer %N2% to %N3%
Variable Modify Integer: Inc (%N2%)
 End If
 Variable Set Integer %N1% with a Random Number
 If Variable %N1% <> variable %N2%
Variable Modify Integer: Convert %N1% to text string %T1%
Variable Modify String: Save %T1% to Text File
Break
 End If
Repeat End
Text Box Display: This Text Box Represents The Rest of Your Macro

 

<REM2:%N1% Random Number><REM2:%N2% Previously Generated Random Number><REM2:%N3% Maximum Value of Randomly Generated Number><REM2:%T1% Text Variable Used to Facilitate New and Old Number Comparisons><REM2:%HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.><REM2:This may be different for every computer, so I used these defaults because they should work for anybody.><IVAR2:03:01:10><REP3:08:000002:000001:0001:1:01:T1><IFOTH:01:2:%HOMEDRIVE%%HOMEPATH%/Random Number><TVAR2:01:04:%HOMEDRIVE%%HOMEPATH%/Random NumberT><TMVAR2:05:01:02:000:000:><ELSE><IVAR2:02:01:%N3%><NMVAR:08:02:0:0000001:0:0000000><ENDIF><IVAR2:01:06:%N3%><IFVAR2:5:01:2:N2><NMVAR:05:01:0:0000001:0:0000000><TMVAR2:17:01:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><BREAK><ENDIF><ENDREP><TBOX4:T:1:CenterCenter000278000200:000:This Text Box Represents The Rest of Your MacroCurrent Number: %N1%>

 

And yes, you may remove the final Text Box display... it's just there to let you know where to add the rest of your macro.

Link to post
Share on other sites
thanks! if i simply use %HOMEDRIVE% without the %HOMEPATH%, then it will put the file directly into C: drive right? is %HOMEDRIVE% always C: drive, even if I have multiple drives? is the file called "random number"?

%HOMEDRIVE% is probably your C: drive, though it could be different. I honestly don't know how to change it... I just know that at home, it is my C: drive, but at work it is my H: drive.

 

You're welcome to change the path to anything you want. The macro command itself has a Browse button so you can browse to the exact folder you want the file to be created in. I just used the %HOMEDRIVE%%HOMEPATH% text to make the macro immediately useable for anybody who copied the script directly into a new macro.

 

The file name is Random Number, yes. Note that I did not add an extension (like .txt). So the file may not show up if you browse down the path. You can change the macro to create the file as Random Number.txt if you would like. It will not affect the macro's performance either way.

Link to post
Share on other sites
cool thanks. :) would it be any more difficult to make the macro not repeat the previous 5 numbers, instead of just 1?

A little bit. You'll need to establish some logic with regards to the numbers being saved/retrieved from the Random Number file. When I get a few minutes I'll play with it and see what I can do.

Link to post
Share on other sites
cool thanks. :) would it be any more difficult to make the macro not repeat the previous 5 numbers, instead of just 1?

This should do it.

 

I had to re-write a good portion of the macro, so a lot of the variables in this one don't correspond to the variables used in the previous version.

 

For example: N3 is no longer the "maximum" value, N2 is. Just something to keep in mind as you play with it.

 

Oh, one more thing. If you want to increase the quantity of generated numbers before a repeat is permitted, you'll want to change the command line:

 

If Variable %N4% = 5

Just change the 5 to something else... If you increase it, then later decide to decrease it, you will need to either: a) Write some code within the macro that will

eliminate all but the last however-many-you-want-to-keep numbers, or b ) Simply delete the Random Number file and let the macro start from scratch (my

personal choice).

 

// %N1% Random Number
// %N2% Maximum Value of Randomly Generated Number
// %N3% The Length of Variable T1
// %N4% The Length of Variable T2
// %N5% The Position of the First Blank Space Within T3
// %N6% The Length of Variable T3
// %T1% Text Variable Holding Last 5 Random Numbers. Used For Comparison With N1/T4 (New Number)
// %T2% A Duplicate of T1, Used to Calculate Quantity of Previous Numbers
// %T3% Another Duplicate of T1, Used For the Same Purpose
// %T4% The Text Variable Conversion of N1 (Randomly Generated Number). Used To Compare With T1.
// %HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.
// This may be different for every computer, so I used these defaults because they should work for anybody.
Variable Set Integer %N2% to 10
Repeat Until %T1% <> %T1%
 If File Exists "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Set String %T1% from File: "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Modify String: Trim %T1%
Variable Set Integer %N3% from Length of Variable %T1%
Variable Modify Integer: Inc (%N3%)
Variable Modify String: Pad Right %T1%
Variable Set String %T2% "%T1%"
Replace " " with "" in %T2%
Variable Set Integer %N4% from Length of Variable %T2%
If Variable %N4% = 5
  Variable Set String %T3% "%T1%"
  Variable Set Integer %N5% from Position of Text in Variable %T3%
  Variable Modify String: Delete Part of %T3%
  Variable Modify String: Trim %T3%
  Variable Set Integer %N6% from Length of Variable %T3%
  Variable Modify Integer: Inc (%N6%)
  Variable Modify String: Pad Right %T3%
  Variable Modify String: Save %T3% to Text File
Else
  Variable Modify String: Save %T1% to Text File
End If
 End If
 Variable Set Integer %N1% with a Random Number
 Variable Modify Integer: Convert %N1% to text string %T4%
 If Variable %T1% does not contain variable %T4%
Variable Set String %T4% "%T4% "
Variable Modify String: Append %T4% to Text File
Break
 End If
Repeat End
Text Box Display: This Text Box Represents The Rest of Your Macro

<REM2:%N1% Random Number><REM2:%N2% Maximum Value of Randomly Generated Number><REM2:%N3% The Length of Variable T1><REM2:%N4% The Length of Variable T2><REM2:%N5% The Position of the First Blank Space Within T3><REM2:%N6% The Length of Variable T3><REM2:%T1% Text Variable Holding Last 5 Random Numbers. Used For Comparison With N1/T4 (New Number)><REM2:%T2% A Duplicate of T1, Used to Calculate Quantity of Previous Numbers><REM2:%T3% Another Duplicate of T1, Used For the Same Purpose><REM2:%T4% The Text Variable Conversion of N1 (Randomly Generated Number). Used To Compare With T1.><REM2:%HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.><REM2:This may be different for every computer, so I used these defaults because they should work for anybody.><IVAR2:02:01:10><REP3:08:000002:000001:0001:1:01:T1><IFOTH:01:2:%HOMEDRIVE%%HOMEPATH%/Random Number><TVAR2:01:04:%HOMEDRIVE%%HOMEPATH%/Random NumberT><TMVAR2:01:01:00:000:000:><IVAR2:03:12:1><NMVAR:08:03:0:0000001:0:0000000><TMVAR2:15:01:00:%N3%:000:><TVAR2:02:01:%T1%><TMVAR2:21:02:01:000:000: ><IVAR2:04:12:2><IFVAR2:2:04:1:8><TVAR2:03:01:%T1%><IVAR2:05:13:3: ><TMVAR2:11:03:00:001:N05:><TMVAR2:01:03:00:000:000:><IVAR2:06:12:3><NMVAR:08:06:0:0000001:0:0000000><TMVAR2:15:03:00:%N6%:000:><TMVAR2:17:03:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><ELSE><TMVAR2:17:01:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><ENDIF><ENDIF><IVAR2:01:06:%N2%><NMVAR:05:01:0:0000004:0:0000000><IFVAR2:4:01:8:T4><TVAR2:04:01:%T4% ><TMVAR2:20:04:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><BREAK><ENDIF><ENDREP><TBOX4:T:1:CenterCenter000278000200:000:This Text Box Represents The Rest of Your MacroCurrent Number: %N1%

%T1%>

Link to post
Share on other sites

there seems to be a problem with the macro. it usually only leaves 4 numbers in the random file. if i execute the macro a lot of times, at some point, a lot of numbers get added--and it continues to add thereafter. im using random numbers beyond 10. it seems to be due to the length of the double digit numbers vs the single digit numbers. the problems starts to happen after a single digit number gets added amongst the double digit numbers. can this be fixed?

Link to post
Share on other sites
there seems to be a problem with the macro. it usually only leaves 4 numbers in the random file. if i execute the macro a lot of times, at some point, a lot of numbers get added--and it continues to add thereafter. im using random numbers beyond 10. it seems to be due to the length of the double digit numbers vs the single digit numbers. the problems starts to happen after a single digit number gets added amongst the double digit numbers. can this be fixed?

Yes, it can be fixed. I'll take a look at the code and see if I can debug it. It was working for me, but then I may have tweaked something during a test that didn't get un-tweaked. I can't do it right now, but I'll try to get to it at some point this week-end.

Link to post
Share on other sites
any luck with fixing that macro so far?

Sorry, not yet. It's been a really long weekend. Holiday on Saturday, and I was out of town all day yesterday. I'm just about to head to work. Because I tend to tweak macros all day long at work anyway, I will most likely be able to find some time to experiment with this one today.

Link to post
Share on other sites
any luck with fixing that macro so far?

OK, I did some tests and saw what I believe was wrong. I forgot to add a relatively important line of text that actually caclulated how many previous numbers

were in the "Random Number" file.

 

I've fixed that, and run over 100 successful tests with values ranging from 0-50.

 

I do have one caveat about this macro that I didn't mention before, but probably bears mentioning.

 

When you're setting the value for how often a number is allowed to repeat, you may want to set the If Variable 1 digit higher (in this iteration of the

macro it would be %N5%).

 

For example:

 

With the command: If Variable N5 = 5

Then

If the previous 5 numbers were 1, 2, 3, 4, 5

Then the next number could be 1

 

If you don't want 1 to be a possibility, set the If Varibable N5 = to 6.

 

Ok, here's the adjusted macro. It really should work. If not, let me know and I'll take another look.

 

// %N1% Random Number
// %N2% Maximum Value of Randomly Generated Number
// %N3% The Length of Variable T1
// %N4% The Length of Variable T2
// %N5% The Quantity of Previously Generated Numbers on File
// %N7% The Position of the First Blank Space Within T3
// %N8% The Length of Variable T3
// %T1% Text Variable Holding Last 5 Random Numbers. Used For Comparison With N1/T4 (New Number)
// %T2% A Duplicate of T1, Used to Calculate Quantity of Previous Numbers
// %T3% Another Duplicate of T1, Used For the Same Purpose
// %T4% The Text Variable Conversion of N1 (Randomly Generated Number). Used To Compare With T1.
// %HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.
// This may be different for every computer, so I used these defaults because they should work for anybody.
Variable Set Integer %N2% to 50
Repeat Until %T1% <> %T1%
 If File Exists "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Set String %T1% from File: "%HOMEDRIVE%%HOMEPATH%/Random Number"
Variable Modify String: Trim %T1%
Variable Set Integer %N3% from Length of Variable %T1%
Variable Modify Integer: Inc (%N3%)
Variable Modify String: Pad Right %T1%
Variable Set String %T2% "%T1%"
Replace " " with "" in %T2%
Variable Set Integer %N4% from Length of Variable %T2%
Variable Modify Integer: %N5% = %N3% - %N4%
If Variable %N5% = 8
  Variable Set String %T3% "%T1%"
  Variable Set Integer %N7% from Position of Text in Variable %T3%
  Variable Modify String: Delete Part of %T3%
  Variable Modify String: Trim %T3%
  Variable Set Integer %N8% from Length of Variable %T3%
  Variable Modify Integer: Inc (%N8%)
  Variable Modify String: Pad Right %T3%
  Variable Modify String: Save %T3% to Text File
Else
  Variable Modify String: Save %T1% to Text File
End If
 End If
 Variable Set Integer %N1% with a Random Number
 Variable Modify Integer: Convert %N1% to text string %T4%
 If Variable %T1% does not contain variable %T4%
Variable Set String %T4% "%T4% "
Variable Modify String: Append %T4% to Text File
Break
 End If
Repeat End
Variable Set String %T1% from File: "%HOMEDRIVE%%HOMEPATH%/Random Number"
Text Box Display: This Text Box Represents The Rest of Your Macro

<REM2:%N1% Random Number><REM2:%N2% Maximum Value of Randomly Generated Number><REM2:%N3% The Length of Variable T1><REM2:%N4% The Length of Variable T2><REM2:%N5% The Quantity of Previously Generated Numbers on File><REM2:%N7% The Position of the First Blank Space Within T3><REM2:%N8% The Length of Variable T3><REM2:%T1% Text Variable Holding Last 5 Random Numbers. Used For Comparison With N1/T4 (New Number)><REM2:%T2% A Duplicate of T1, Used to Calculate Quantity of Previous Numbers><REM2:%T3% Another Duplicate of T1, Used For the Same Purpose><REM2:%T4% The Text Variable Conversion of N1 (Randomly Generated Number). Used To Compare With T1.><REM2:%HOMEDRIVE%%HOMEPATH% - Essentially represents C: Drive and your default folder.><REM2:This may be different for every computer, so I used these defaults because they should work for anybody.><IVAR2:02:01:50><REP3:08:000002:000001:0001:1:01:T1><IFOTH:01:2:%HOMEDRIVE%%HOMEPATH%/Random Number><TVAR2:01:04:%HOMEDRIVE%%HOMEPATH%/Random NumberT><TMVAR2:01:01:00:000:000:><IVAR2:03:12:1><NMVAR:08:03:0:0000001:0:0000000><TMVAR2:15:01:00:%N3%:000:><TVAR2:02:01:%T1%><TMVAR2:21:02:01:000:000: ><IVAR2:04:12:2><NMVAR:02:05:1:0000003:1:0000004><IFVAR2:2:05:1:8><TVAR2:03:01:%T1%><IVAR2:07:13:3: ><TMVAR2:11:03:00:001:N07:><TMVAR2:01:03:00:000:000:><IVAR2:08:12:3><NMVAR:08:08:0:0000001:0:0000000><TMVAR2:15:03:00:%N8%:000:><TMVAR2:17:03:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><ELSE><TMVAR2:17:01:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><ENDIF><ENDIF><IVAR2:01:06:%N2%><NMVAR:05:01:0:0000004:0:0000000><IFVAR2:4:01:8:T4><TVAR2:04:01:%T4% ><TMVAR2:20:04:00:000:000:%HOMEDRIVE%%HOMEPATH%/Random NumberF><BREAK><ENDIF><ENDREP><TVAR2:01:04:%HOMEDRIVE%%HOMEPATH%/Random NumberT><TBOX4:T:1:CenterCenter000278000200:000:This Text Box Represents The Rest of Your MacroCurrent Number: %N1%

Previous Numbers: %T1%>

Link to post
Share on other sites
  • 2 weeks later...

Hey there Guys, Steve, how about this, using convert integer to string and if/contains?

 

Oh I guess I didn't really notice [page 2] :wacko: I thought this was still not responded to from a previous post. Oh well, was fun to have a go at it!

 

 

CONCEPT only!!! Totally untested:

 

Repeat Until %T3% = "forever"

// add one to count

Variable Modify Integer: %N2% = %N2% + 1

// repeat until we pull a variable that doesnt match

// using repeat until as a looping 'if contains'

Repeat Until %T10% Does not contain %T1%

// get a number

Variable Set Integer %N1% with a Random Number

// convert to string

Variable Modify Integer: Convert %N1% to text string %T1%

Repeat End

// add the last number to the long string

Variable Modify String: Append "%T1%" to %T10%

// add a space (dont know if this is necessary)

Variable Modify String: Append " " to %T10%

// check for number of integers written to string

If Variable %N2% >= 5

// reset count

Variable Set Integer %N1% to 0

// clean long string

Clear Text Variables: From 10 To 10

End If

// external functions/text box

Text Box Display: Number is: %N1%

Repeat End

 

<REP3:08:000001:000001:0003:0:01:forever><REM2:add one to count><NMVAR:01:02:1:0000002:2:0000001><REM2:repeat until we pull a variable that doesnt match><REM2:using repeat until as a looping 'if contains'><REP3:08:000008:000001:0010:1:01:T1><REM2:get a number><IVAR2:01:06:100><REM2:convert to string><NMVAR:05:01:0:0000001:0:0000000><ENDREP><REM2:add the last number to the long string><TMVAR2:07:10:00:000:000:%T1%><REM2:add a space (dont know if this is necessary)><TMVAR2:07:10:00:000:000: ><REM2:check for number of integers written to string><IFVAR2:2:02:5:5><REM2:reset count><IVAR2:01:01:0><REM2:clean long string><CLEARVAR1:T:10:10><ENDIF><REM2:external functions/text box><TBOX4:T:1:CenterCenter000278000200:000:Number is: %N1%Text string should match: %T1%

 

Long string is:

%T10%><ENDREP>

Link to post
Share on other sites
  • 3 months later...
the macro was working great on winxp. but now i upgraded to windows 7 x64 and i get an error message about unable to create the .txt file in C: drive. im admin on the system. any ideas?

Windows 7 does not allow anyone, admins included, to create a file on the root of the c: drive without a UAC prompt. Modify your macro to use a folder that Windows 7 does not protect.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...