Jump to content


Photo

Random number generator


  • Please log in to reply
6 replies to this topic

#1 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 281 posts
  • Version:GM8

Posted 25 January 2013 - 07:43 AM

Well hello there. I was asked for help with a script to generate a random 12 digit number as a string, and thought the GMC could make use of it too.

You could use it to make serial numbers, or random seeds in a generated game. What ever the use, it's up to you.

//Generates a set amount of random numbers
//Script : number_generate(string_length)
var num,str;
num = 0;
str = "";

repeat(argument0){
    num = choose(0,1,2,3,4,5,6,7,8,9);
    str += string(num);
}

return(str);

It returns the string that is generated from the numbers. Argument0 is the amount of digits you want in the string.

Hope someone else finds it useful.
  • 0

#2 YellowAfterlife

YellowAfterlife

    GMC Member

  • Global Moderators
  • 3821 posts
  • Version:GM:Studio

Posted 25 January 2013 - 09:07 AM

Can be simplified...
var str;
str = "";
repeat (argument0) str += string(irandom_range(0, 9));
return str;
Generally it looks like something that majority of users should be able to come up with.
  • 0

#3 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 281 posts
  • Version:GM8

Posted 25 January 2013 - 09:54 AM

Haha I predicted someone would have a better, easier way of doing this. And you were one of the top contestants :P. Thanks. I didnt know about irandom_range(). Im guessing it's for "integer" or whole numbers?
  • 0

#4 paul23

paul23

    GMC Member

  • Global Moderators
  • 3802 posts
  • Version:GM:Studio

Posted 25 January 2013 - 11:14 AM

var num = 12;
string(floor(random(power(10,num))));

just missing the leading zeros.

var num = argument0;
var r = floor(random(power(10,num)));
num -= floor(log10(r));
num -= 1;
return string_repeat("0",n) + string(r);

  • 0

#5 HelpMyFellowPeople

HelpMyFellowPeople

    GMC Member

  • GMC Member
  • 281 posts
  • Version:GM8

Posted 25 January 2013 - 07:57 PM

It's all good and fine shortening something as much as you can, but isnt that the whole point of a script? So you dont have to type out the whole bunch of code? So really the size of a script makes no difference except maybe in the execution speed. Though it is good learning experience seeing different ways of doing things.
  • 0

#6 paul23

paul23

    GMC Member

  • Global Moderators
  • 3802 posts
  • Version:GM:Studio

Posted 27 January 2013 - 01:35 AM

It's all good and fine shortening something as much as you can, but isnt that the whole point of a script? So you dont have to type out the whole bunch of code? So really the size of a script makes no difference except maybe in the execution speed. Though it is good learning experience seeing different ways of doing things.

Well as a non-looping code example it will definitely run faster. Much less branches, and can be translated quite easily to assembly.
  • 0

#7 Gametester1000

Gametester1000

    GameTester1000

  • GMC Member
  • 149 posts
  • Version:GM:Studio

Posted 27 January 2013 - 05:40 AM

2 Mods ganging up on 1 person :whistle:
  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users