944,179 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1325
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 5th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by ov3rcl0ck ...
Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Generating random nos without using random module
Is there any other method or logic except that formula?
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.

http://en.wikipedia.org/wiki/List_of_algorithms

>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

These all will help you to seed......but to generate sets you need some method!
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 5th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vishesh ...
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.

http://en.wikipedia.org/wiki/List_of_algorithms

>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

These all will help you to seed......but to generate sets you need some method!
I have searched wikipedia n saw many other algos. Rest are too compicated.

Wats the meaning of seed?

n sets refers to the list of random nos?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Generating random nos without using random module
This Python code shows you how to pick a random number from a list ...
python Syntax (Toggle Plain Text)
  1. # picking random numbers from a list
  2. # Python2 syntax
  3.  
  4. def pick_rand(rand_list, start=-1, incr=1):
  5. """
  6. closure based function that cycles through rand_list one index
  7. at a time for each call, index resets to zero at end of list
  8. """
  9. def inner():
  10. inner.index += incr
  11. if inner.index >= len(rand_list):
  12. inner.index = 0
  13. return rand_list[inner.index]
  14. inner.index = start
  15. return inner
  16.  
  17. # random number list of 100 numbers from 0 to 99
  18. # created by timing user input events
  19. rand_list = [
  20. 6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
  21. 12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
  22. 9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
  23. 59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
  24. 50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
  25. 53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15
  26. ]
  27.  
  28. # testing only ...
  29. print len(rand_list) # 100
  30. print "average = %d" % (sum(rand_list)//len(rand_list)) # 48
  31.  
  32. # needed!
  33. rand = pick_rand(rand_list)
  34.  
  35. # show 10 random numbers
  36. for k in range(10):
  37. print rand(),
  38.  
  39. print
  40.  
  41. # show another 10 random numbers
  42. for k in range(10):
  43. print rand(),
The list is generated this way ...
python Syntax (Toggle Plain Text)
  1. # a time based random number generator
  2. # that uses the random time between a user's input events
  3. # to create a list of random numbers
  4. # Python2 syntax
  5.  
  6. import time
  7.  
  8. def random_number(low, high):
  9. """
  10. a time based random number generator
  11. uses the random time between a user's input events
  12. returns an integer between low and high-1
  13. """
  14. return int(low + int(time.time()*1000) % (high - low))
  15.  
  16. low = 0
  17. high = 100
  18. count = 0
  19. rand_list = []
  20. print "Press enter for a random number (q to quit):"
  21. while True:
  22. sel = raw_input()
  23. if sel == 'q':
  24. break
  25. rand = random_number(low, high)
  26. rand_list.append(rand)
  27. # give user feedback
  28. print "%d --> %d" % (count, rand)
  29. count += 1
  30.  
  31. print rand_list
  32.  
  33. """my rand_list 0f 100 random numbers between 0 and 99 -->
  34. [6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
  35. 12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
  36. 9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
  37. 59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
  38. 50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
  39. 53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15]
  40. """
I made the rand_list relatively short for this example, normally you may want to go for a length of >1000.
Last edited by vegaseat; Nov 5th, 2009 at 1:36 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 9th, 2009
0
Re: Generating random nos without using random module
Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
Fetching memory usage and mouse input are OS dependent. There isn't a library to fetch memory usage, but you can use the "mem" command on windows to fetch it, and the "ps" or "free" command on UNIX to fetch it, but keep in mind you might need to use a little regex or at least some string functions. For mouse input you can use pyHook for windows, and xlib for UNIX(OS X supports X11, and any Unix distro worth using supports it).

If you need me to I can post some examples of pyHook, xlib, and memory fetching.
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009
Nov 12th, 2009
0
Re: Generating random nos without using random module
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.

thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 12th, 2009
0
Re: Generating random nos without using random module
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.

thanks again
Well, once you have it down to Python code, let us know.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 12th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vegaseat ...
Well, once you have it down to Python code, let us know.

yes sir!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to use MD5 in Python?
Next Thread in Python Forum Timeline: how to use getpass in python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC