Skip to content

How to random a string or a number

Vex Woo edited this page Jul 13, 2016 · 3 revisions

This document talks about how to generate a random string / number in the cleanest way possible.

To generate a random string, you need to import the library first. The following is an example of using the library in python interactive shell.

>>> from pocsuite.lib.utils import randoms

It can also be used in poc(s) files, and has core functions as follow:

  • randoms.allchars
  • randoms.lowerAlpha
  • randoms.upperAlpha
  • randoms.numerals
  • randoms.rand_base
  • randoms.rand_char
  • randoms.rand_item_from_iters
  • randoms.rand_text
  • randoms.rand_text_alpha
  • randoms.rand_text_alpha_lower
  • randoms.rand_text_alpha_upper
  • randoms.rand_text_alphanumeric
  • randoms.rand_text_numeric

rand_base

generate a random string with chars collection

>>> allchars = ['a', 'b', 'c', 'x', 'y', 'z']
>>> randoms.rand_base(4, 'abcdef', allchars)
'zxxx'

rand_char

generate a random char with chars collection

>>> chars = [randoms.rand_char() for i in range(5)]
>>> chars
['j', '\x9a', '.', '\xa7', '\x82']

rand_text

generate a random string (cab be with unprintable chars)

>>> randoms.rand_text(4)
'\xbb\xfeF!'

rand_text_alpha

generate a random string with alpha chars

>>> randoms.rand_text_alpha(4)
'vPtf'

rand_text_alpha_lower

generate a random lower string with alpha chars

>>> randoms.rand_text_alpha_lower(4)
'fhwr'

rand_text_alpha_upper

generate a random upper string with alpha chars

>>> randoms.rand_text_alpha_upper(4)
'GAZT'

rand_text_alphanumeric

generate a random string with alpha and numerals chars

>>> randoms.rand_text_alphanumeric(4)
'4xZT'

rand_text_numeric

generate a random string with numerals chars

>>> randoms.rand_text_numeric(4)
'5386'

rand_item_from_iters

choose a random item from items

>>> abc = ['aa', 'bb', 'cc', 'dd']
>>> randoms.rand_item_from_iters(abc)
'dd'
>>> randoms.rand_item_from_iters(abc)
'bb'