Fedora Linux Support Community & Resources Center
  #1  
Old 2nd December 2015, 09:40 AM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 37
Posts: 4,280
linuxfirefox
Programming Challenge: Create a list of X random numbers between Y and Z

Here is a new one.

Create a list of random numbers.
Very similar, but not identical to permutation list.

Rules:
1. All numbers must be random
2. No number may occour more than once
3. Every new list differs from the previous

Code:
./tui-rnd -l 5 10 20
17 19 15 18 11

 ./tui-rnd -l 5 10 20
15 13 16 11 19

 ./tui-rnd -l 3 100 200
110 182 184

./tui-rnd -l 3 100 200
184 193 175
And here's the code:
Code:
	"-l"|"--list")
		doList=true
		LIST_NUM="$2"
		shift 2
		;;
<...>
	esac
	case $# in
	0)	MIN=${MIN:-1}
		MAX=${MAX:-100}
		;;
	1)	MIN=${MIN:-1}
		MAX=${MAX:-$1}
		;;
	2)	MIN=${MIN:-$1}
		MAX=${MAX:-$2}
		;;
	esac
#
#	Functions
#
	myRND() { #
	#
	#
		VAR=$[ $RANDOM % $MAX ]
		while  [ $VAR -lt $MIN ] || [ $VAR -gt $MAX ] 
		do	VAR=$[ $RANDOM % $MAX ]
			# This is required, otherwise one never gets the 'max' value.
			VAR=$(( $VAR + 1 ))
		done
		printf "$VAR\n"
	}
#
#	Display & Action
#
#
	if $doList
	then	declare -A L
		while [ $c -lt $LIST_NUM ]
		do
			n=$(myRND)
			while echo "${L[@]}" | $GREP -q $n
			do	n=$(myRND)
			done
			L[$c]=$n
			((c++))
		done
		echo "${L[@]}"
		unset L
	elif $doPerm
<...> ## See other thread :p ##

Be nasty:
Code:
./tui-rnd -l $(( $(./tui-rnd) / 2 ))  $(./tui-rnd) $(( 100 + $(./tui-rnd) ))
79 75 94 64 137 96 116 87 159 67 77 136 150 61 58 142 89 45 40

./tui-rnd -l $(( $(./tui-rnd) / 2 ))  $(./tui-rnd) $(( 100 + $(./tui-rnd) ))
28 101 54 47

./tui-rnd -l $(( $(./tui-rnd) / 2 ))  $(./tui-rnd) $(( 100 + $(./tui-rnd) ))
114 101 89 18 108 111 46 56 99 29 102 76 79 35 36 109 21 66 63 107 100 67 85 120 37 94 27

Checkpoint:
Obviously, to some extend - in the end, one must also be able to create a permutation list:
Code:
./tui-rnd -l 4 100 103
103 101 102 100

./tui-rnd -l 4 100 103
101 103 102 100

./tui-rnd -l 4 100 103
100 101 102 103
Have fun
__________________
EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161
Reply With Quote
  #2  
Old 2nd December 2015, 11:18 AM
ocratato Online
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 2,499
linuxfirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

Here is a C++ solution

Code:
#include <iostream>
#include <random>
#include <string>

using namespace std;

int main( int argc, char *argv[])
{
        if ( argc != 4 ) return 1;
        int n = stoi(argv[1]);
        double a = stod(argv[2]);
        double b = stod(argv[3]);

        // uniformly-distributed integer random number generator 
        // that produces non-deterministic random numbers.
        std::random_device rd;

        // random number engine based on Mersenne Twister algorithm
        std::mt19937 gen(rd());

        // Produces random floating-point values uniformly distributed on the interval
        std::uniform_real_distribution<> dis(a, b);
        for (int i = 0; i < n; ++i)
        {
                std::cout << dis(gen) << ' ';
        }
        std::cout << '\n';
        return 0;
}
Compile with
Code:
g++ -std=c++11 -o randseq randseq.cpp
Results
Code:
[brenton@acer Forum]$ ./randseq 5 10 20
18.7714 12.3418 15.0423 18.3541 15.3164 
[brenton@acer Forum]$ ./randseq 5 10 20
12.2216 17.727 10.0585 15.7868 10.4973
__________________
Has anyone seriously considered that it might be turtles all the way down?
That's very old fashioned thinking.
The current model is that it's holographic nested virtualities of turtles, all the way down.
Reply With Quote
  #3  
Old 3rd December 2015, 03:24 AM
volgon Offline
Registered User
 
Join Date: May 2005
Posts: 26
linuxfedorafirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

This is a Python 2.7+ solution:
Code:
from __future__ import print_function
from random import randint
import sys


def main(argv):
    if(len(argv) < 3):
        usage()
    else:
        try:
           length = int(argv[0])
           min = int(argv[1])
           max = int(argv[2])
        except:
           usage()

    if(min > max)|(length - 1 > max - min):
        usage()

    nl = list(range(min,max+1))

    for i in range(0,length):
        n = randint(0, len(nl) - 1)
        print(nl[n], end=' ')
        nl.pop(n)
    print()


def usage():
    print("Usage: randseq.py length min max")
    sys.exit(2)

if __name__ == '__main__':
    main(sys.argv[1:])
with output that looks like:
Code:
[volgon@venus python]$ python randseq.py 8 500 75000
31182 28484 4064 46339 29529 8015 30103 55026 
[volgon@venus python]$ python randseq.py 8 500 75000
53221 58680 5498 35058 30803 22929 30383 19578 
[volgon@venus python]$ python3 randseq.py 8 500 75000
19687 53249 57195 8621 43500 38359 72101 14163

Last edited by volgon; 3rd December 2015 at 04:13 PM.
Reply With Quote
  #4  
Old 3rd December 2015, 05:19 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,517
linuxfedorafirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

Here's an example in Octave.
Save this as random_sea.m:
Code:
x = cellfun(@str2num, argv()(1:3));
disp(randperm(x(3)-x(2)+1,x(1))+x(2)-1)
Sample output:
Code:
$ octave -qf random_sea.m 4 100 103
   101   103   100   102

$ octave -qf random_sea.m 4 100 103
   100   101   102   103

$ octave -qf random_sea.m 8 500 75000
   47131   38073    1978    8025   70005    4246   67900   64884

$ octave -qf random_sea.m 8 500 75000
   69939   52525   49934   30930   49814   68498   44371   42827
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #5  
Old 5th December 2015, 11:19 PM
dd_wizard Offline
Registered User
 
Join Date: Sep 2009
Posts: 1,995
linuxfedorafirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

In DrRacket scheme:

Code:
> (define (random-sub-list n min max)
  (take (shuffle (build-list (add1 (- max min)) (lambda (i) (+ i min)))) n))

> (random-sub-list 9 100 200)
'(130 109 102 158 127 149 120 113 123)

> (random-sub-list 5 12 99)
'(83 84 67 68 40)
For those not familiar with lisp and it's derivatives, scheme uses pre-fix notation. Working from the inside out:

(lambda (i) (+ i min)) defines a procedure of one argument that adds min to it's argument.

(add1 (- max min)) adds 1 to max - min

Those two arguments are used by (build-list n proc). Build-list passes n arguments to proc, starting at 0, and returns the results as a list.

That list is shuffled by shuffle.

Finally, (take list n) returns the first n elements of list.

dd_wizard
Reply With Quote
  #6  
Old 12th December 2015, 10:46 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,517
linuxfedorafirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

Here's an example in Julia.

Save this as random_sea.jl:
Code:
num = parse(Int128, ARGS[1]);
from = parse(Int128, ARGS[2]);
to = parse(Int128, ARGS[3]);
display(shuffle(collect(1:to-from+1))[1:num]'+from-1)
println()
Sample output:
Code:
$ julia random_sea.jl 4 100 103
1x4 Array{Int128,2}:
 101  100  102  103

$ julia random_sea.jl 4 100 103
1x4 Array{Int128,2}:
 100  102  103  101

$ julia random_sea.jl 8 500 75000
1x8 Array{Int128,2}:
 19911  37828  41432  11557  50357  10093  40811  63216

$ julia random_sea.jl 8 500 75000
1x8 Array{Int128,2}:
 63579  4356  67966  66035  5039  27180  20342  60381
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #7  
Old 14th December 2015, 05:28 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,517
linuxfedorafirefox
Re: Programming Challenge: Create a list of X random numbers between Y and Z

Here's an example in D, using the LDC compiler (dnf install ldc).

Save this as random_sea.d:
Code:
import std.stdio, std.random, std.range, std.conv;
void main(in string[] args) {
   auto s = iota(to!int(args[2]), to!int(args[3])+1).array;
   randomShuffle(s);
   writeln(randomSample(s, to!int(args[1])));
}
Compile it like this:
Code:
ldc2 random_sea.d
Sample results:
Code:
$ ./random_sea 4 100 103
[102, 103, 100, 101]

$ ./random_sea 4 100 103
[100, 102, 101, 103]

$ ./random_sea 8 500 75000
[54845, 51407, 17009, 17653, 29417, 51525, 34474, 49239]

$ ./random_sea 8 500 75000
[63575, 27631, 55087, 58122, 4239, 31875, 23883, 7673]
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
Reply

Tags
challenge, create, list, numbers, programming, random

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming challenge: Create a GUI window RupertPupkin Programming & Packaging 133 20th February 2017 08:20 AM
Programming challenge: Create a random permutation RupertPupkin Programming & Packaging 84 27th April 2016 09:23 PM
A programming challenge. lsatenstein Programming & Packaging 3 4th January 2015 03:04 AM
Programming Challenge: Comparing Numbers in a String Flounder Programming & Packaging 23 10th January 2014 02:53 PM
Programming challenge: Create a chart RupertPupkin Programming & Packaging 28 18th September 2013 12:14 AM


Current GMT-time: 05:36 (Friday, 26-05-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat