Random Number String Generator

Post your working scripts, libraries and tools
ThunderSnow

Random Number String Generator

12 Aug 2018, 22:25

I have created a script that generates an array of random numbers and builds a string that contains the random numbers separated by commas.

I was building this to see if I could autopopulate an excel spreadsheet with some data that I could then try to manipulate but I got pretty hung up on this part so I'm glad I finally got it. :D

Maybe someone else would find this useful, I'm new to AHK so apologies if I did stuff in weird ways. I looked around but I didn't see anybody did exactly something like this. It seems weird to me you have to create a second variable for the second loop and you can't name it the same as the first variable. How do you do something like n = n + 1 then? I'm more used to how it is in Java, this language is a whole 'nother ballgame for me!

Code: Select all

#SingleInstance Force

n := 10 ;


Arr := Array() ;
Loop %n%
{
Random rand, 1, 100 ;
R_%A_index% := rand ;
}

str = %R_1% ;
n1 := n - 1 ;
loop %n1%
{
index := A_index + 1 ;
str1 := R_%index% ;
str = %str%`, %str1% ;
}

msgbox, % str ;
User avatar
TheDewd
Posts: 1135
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Random Number String Generator

13 Aug 2018, 16:06

I think this is what you are trying to do... Wanted to give you another example.

Code: Select all

#SingleInstance, Force ; Allow only one running instance of script

N := 10 ; How many random numbers to generate
RandStr := "" ; Initialize variable to store combined random number string

Loop, % N { ; Loop [N] times
	Random, Rand, 1, 100 ; Return number from given range
	RandStr .= Rand (A_Index = N ? "" : ", ") ; Append value to existing variable contents (in this case, it's initially an empty string). Used ternary to specify whether or not to add a comma... If A_Index = 10 (last number) don't add comma, else add the comma.
}

MsgBox, % RandStr ; Display string in MessageBox
Other examples related to your inquiries:

Code: Select all

N := 0

Loop, 20 {
	N++ ; Increment value of N by 1 (can do N-- to subtract 1)
	MsgBox, % N
}

; ----------------------

N := 10
Str := "Hello"

Loop, % N {
	Str .= (A_Index = N ? "" : " " A_Index)
}

MsgBox, % Str
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: grimboto, ptpt, Xtra and 39 guests