Jump to content

Closed-----Number generator with specific number


Wena
 Share

Recommended Posts

Hi All

I need to generate 12 random numbers using a specific number and the sum of the 12 random numbers must = the specific number.

IE:

12000 \ 12 = 1000

1000 must be randomised.

Randomised numbers added together must equal 12000.

I have tried (With a loop of 12) Random ( [Min [, Max [, Flag]]] ) with $Min = 600 and $Max = 1400 but sometimes the sum of the 12 random numbers is lower than 12000 and somtimes higher.

Any Clues for me.

I do not expect anyone to write it for me but a point in the right direction would be of great help.

Thanks in advance

Alistair

Edited by Wena
Link to comment
Share on other sites

Generate only 11 numbers as you did, then the 12th will need to be = the expected sum minus the actual sum of the 11.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

This code will generate numbers and keep subtracting from the total until the last number, which will be what is left over.

#include <array.au3>
$finalNum = 12000
$addUpArray = 0
Dim $num[12]
 
For $i = 0 to 10 Step 1
    $num[$i] = Random(0, $finalNum - $addUpArray, 1)
    $addUpArray = $addUpArray + $num[$i]
Next
 
$num[11] = $finalNum - $addUpArray
_ArrayDisplay($num)
 
$finalTotal = $addUpArray + $num[11]
 
MsgBox(0, "", "And the final total is: " & $finalTotal)

However, that code produces large numbers to small. For more consistent random numbers, try this:

#include <array.au3>
 
$finalNum = 12000
 
$restart = 0
Dim $num[12]
 
Do
$addUpArray = 0
For $i = 0 to 10 Step 1
    $num[$i] = Random(0, $finalNum / 6, 1)
    $addUpArray = $addUpArray + $num[$i]
Next
If $addUpArray > 12000 Then
    $restart = 1
Else
    $restart = 0
EndIf
Until $restart = 0
$num[11] = $finalNum - $addUpArray
_ArrayDisplay($num)
 
$finalTotal = $addUpArray + $num[11]
 
MsgBox(0, "", "And the final total is: " & $finalTotal)

#include <ByteMe.au3>

Link to comment
Share on other sites

HI...Apologies for the late reply.

@JCHD-Thanks, although I did not use your method, it did give me a new look at the way I was writing my script and eventually got it working. Thank You

@Sleepydrdr - I wish I had seen your post early it would have halved the length of my script. Thank you..

Cheers thanks guys.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...