1

I am a beginner in programming. I need a program that generate a random number between 1 - 100 in each second. This is the code I wrote with simple AHK Script.

Loop
{
 Random, rand_num, 1, 100
ToolTip, Random number:`n%rand_num%`n`nPress ESC to close
Sleep, 1000
}

ESC::ExitApp

The result is okay. But i think their are way better options to solve this task. the program don't run smooth. so when i move the mouse, the field with the number is lagging. or lagging is maybe the wrong word. it doesn't move real time with the mouse. and a second think is the => press escape to close text. is their a easy way to remove this? so that the field with the numbers gets a little bit smaller? i have some experience in java and python,maybe its easier to create a program with these languages. but i am beginner. hope someone can help me either with my AHK script or with tips how can i solve the task in another language. thanks for each helping comment

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Coffeeyay
  • 13
  • 3
  • what is java doing in the tag list? – Kumar Saurabh Sep 03 '15 at 12:25
  • One does not simply want to display a random number. What is the use of this, do you need an UI ? Why java/python ? – Baart Sep 03 '15 at 12:27
  • i have some experience in java and python. so maybe when its easy to creatre a randomprogramm in these languages some1 can give me a startingpoint or some tipps. but im new here. so sry if the taggs are wrong. i didnt find a AHKScripttag. – Coffeeyay Sep 03 '15 at 12:30
  • I need this programm for a strategicgame. I have charts and i play a specific moves with a mixxed strategie. For example one move ill play 65% with move a and 35% with move b. So when i get each second a random number between 1 and 100 i can randomnize my game. my AHK script does the task. but its not the best solution avaible i think. – Coffeeyay Sep 03 '15 at 12:40

2 Answers2

2
SetBatchLines, -1
lastNumTick := A_TickCount
Random, randNum, 1, 100
SetTimer, UpdateRandomNumber, 1

UpdateRandomNumber:
    If (A_TickCount > lastNumTick+1000) {
        Random, randNum, 1, 100
        lastNumTick := A_TickCount
    }
    ToolTip, %randNum%
Return

ESC::ExitApp

Or maybe you prefer that:

SetTimer, UpdateRandomNumber, 1000

UpdateRandomNumber:
    Random, randNum, 1, 100
    TrayTip,, %randNum%
Return

ESC::ExitApp
Forivin
  • 14,780
  • 27
  • 106
  • 199
0

Java 7 solution:

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MyRandom {
    public static void main(String[] args) {
        Random random = new Random(System.currentTimeMillis());
        int iRandomValue;

        while (true) {
            // random.nextInt(99) generates number between 0 and 99;
            iRandomValue = 1 + random.nextInt(99);
            System.out.println("Random number: " + iRandomValue);

            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(1));
            } catch (InterruptedException exIgnoreInterrupted) {
            }
        }
    }
}
Talorias
  • 29
  • 3